func onSocket(sock: AsyncSocket!, didConnectToHost host: String!, port: UInt16) {
print("+++++++++++++ onSocket +++++++++++++")
var data:NSData = NSData() ///Users/gameover/Works/Apple/SocketLab/SocketLab/SocketUtil.swift:33:13: Variable 'data' was never mutated; consider changing to 'let' constant
let loginIn = UserLoginIn.Builder()
loginIn.setId(Int32(3))
.setUsername("nothing")
.setPassword("123456")
do {
let loginInBuild = try loginIn.build()
print(try loginInBuild.encode())
let length = loginInBuild.serializedSize()
let msg:NSMutableData = NSMutableData(length: Int(length + length.computeInt32SizeNoTag()))!
let stream = CodedOutputStream(data: msg)
try loginInBuild.writeToCodedOutputStream(stream)
try stream.writeRawData(data)
print(NSString(data: data, encoding: NSUTF8StringEncoding))
print("\(data.length) \(loginInBuild.serializedSize())") // this output: 0 19 why?
} catch {
print(error)
}
client.writeData(data, withTimeout: 0, tag: 0)
client.readDataWithTimeout(10, tag: 0)
}
Wenn danach: try stream.writeRawData (data); die Datenlänge noch 0; Wie schreibe ich es in NSData und fügen Sie eine Paketlänge in der Paketkopfzeile hinzu. Ich werdeIch kann Stream nicht mit Protobuf-swift CodeOutputStream in NSData schreiben?
try stream.writeInt32NoTag(loginInBuild.serializedSize())
vor
try loginInBuild.writeToCodedOutputStream(stream)
füge ich weiß nicht, ob es richtig?
Irgendwelche hilft? Vielen Dank!