0
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)
}
當在此之後:嘗試stream.writeRawData(data); data.length仍然爲0;如何在NSData中編寫它並在包頭中添加一個包長度。我將添加我不能用protobuf-swift CodeOutputStream寫流到NSData?
try stream.writeInt32NoTag(loginInBuild.serializedSize())
在
try loginInBuild.writeToCodedOutputStream(stream)
前,我不知道這是否正確?
有幫助嗎?謝謝!