坚持使用hasSpaceAvailable并且无法从NSInputStream中读取
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了坚持使用hasSpaceAvailable并且无法从NSInputStream中读取相关的知识,希望对你有一定的参考价值。
我在处理流事件时遇到了问题。我有一个客户端应用程序通过TCP连接到服务器,并不断从服务器接收JSON数据。
到目前为止,我创建了这样的流:
// Create Stream
var readStream : Unmanaged<CFReadStream>?
var writeStream : Unmanaged<CFWriteStream>?
let host : CFString = NSString(string: serverUrl!)
let port : UInt32 = UInt32(serverPort)
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, host, port, &readStream, &writeStream)
inputStream = readStream!.takeUnretainedValue()
outputStream = writeStream!.takeRetainedValue()
inputStream!.delegate = self
outputStream!.delegate = self
inputStream!.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
outputStream!.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
inputStream!.open()
outputStream!.open()
// Send Authentication Data
let uid = 1000000000 + arc4random() % 2000000000
let joinRoomMsg : NSData = "{"roomid": (realRoomID!), "uid": (uid)}".dataUsingEncoding(NSUTF8StringEncoding)!
sendSocketData(outputStream!, totalLength: joinRoomMsg.length + 16, headerLength: 16, protocolVersion: 1, action: 7, param5: 1, data: joinRoomMsg)
这里是sendSocketData
功能:
func sendSocketData(outputStream: NSOutputStream, totalLength: Int32, headerLength: CShort, protocolVersion: CShort, action: Int32, param5: Int32, data: NSData?) {
var totalLengthBE = totalLength.bigEndian
var headerLengthBE = headerLength.bigEndian
var protocolVersionBE = protocolVersion.bigEndian
var actionBE = action.bigEndian
var param5BE = param5.bigEndian
withUnsafePointers(&totalLengthBE, &headerLengthBE, &protocolVersionBE) {
outputStream.write(UnsafePointer($0), maxLength: sizeofValue(totalLengthBE))
outputStream.write(UnsafePointer($1), maxLength: sizeofValue(headerLengthBE))
outputStream.write(UnsafePointer($2), maxLength: sizeofValue(protocolVersionBE))
}
withUnsafePointers(&actionBE, ¶m5BE) {
outputStream.write(UnsafePointer($0), maxLength: sizeofValue(actionBE))
outputStream.write(UnsafePointer($1), maxLength: sizeofValue(param5BE))
}
if data != nil {
outputStream.write(UnsafePointer(data!.bytes), maxLength: data!.length)
}
}
处理:
func stream(aStream: NSStream, handleEvent eventCode: NSStreamEvent) {
if aStream === inputStream {
switch eventCode {
case NSStreamEvent.ErrorOccurred:
print("iStream ErrorOccurred")
case NSStreamEvent.EndEncountered:
print("iStream EndEncountered")
case NSStreamEvent.OpenCompleted:
print("iStream Opened")
case NSStreamEvent.HasBytesAvailable:
print("Stream HasBytesAvailable")
// read data here...
default:
print("iStream Unknow")
}
} else if aStream === outputStream {
switch eventCode {
case NSStreamEvent.OpenCompleted:
print("oStream Opened")
case NSStreamEvent.ErrorOccurred:
print("oStream ErrorOccurred")
case NSStreamEvent.HasSpaceAvailable:
print("oStream HasSpaceAvailable")
default:
print("oStream Unknow")
}
}
}
服务器确实向我发送了JSON数据。
现在我应该能够使用事件Id qazxsw poi从inputStream读取数据。
但问题是:流事件处理程序发送给我NSStreamEvent.HasBytesAvailable
并卡在这里。 NSStreamEvent.HsaSpaceAvailable
返回inputStream.hasBytesAvailable
,我无法阅读任何内容。
据false
说:
如果委托接收到Stream Programing Guide事件并且没有向流写入任何内容,则它不会从运行循环接收更多空间可用事件,直到
NSStreamEvent.HasSpaceAvailable
对象接收到更多字节。发生这种情况时,将重新启动运行循环以获取空间可用事件。
但是我没有数据可以写入流了。
我确实尝试写一些随机数据,但没有运气。我也尝试在NSOutputStream
返回outputStream.hasSpaceAvailable
之后发送身份验证数据,但它也没有做任何事情。
我的代码有什么问题吗?
我终于意识到stream()函数不起作用只是因为此函数的签名不正确/已过时。
这是你正在使用的:
true
但它真的应该是:
func stream(aStream: Stream, handleEvent eventCode: Stream.Event)
以上是关于坚持使用hasSpaceAvailable并且无法从NSInputStream中读取的主要内容,如果未能解决你的问题,请参考以下文章