gpt4 book ai didi

swift - 停留在 hasSpaceAvailable 并且无法从 NSInputStream 读取

转载 作者:可可西里 更新时间:2023-11-01 02:43:26 24 4
gpt4 key购买 nike

我在处理流事件时遇到问题。我有一个客户端应用程序,它通过 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, &param5BE) {
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 数据发回给我。

Screenshot

现在我应该能够使用 eventCode NSStreamEvent.HasBytesAvailable 从 inputStream 读取数据。

但问题是:流事件处理程序向我发送了 NSStreamEvent.HsaSpaceAvailable 并卡在此处。 inputStream.hasBytesAvailable 返回 false 并且我无法读取任何内容。

根据 Stream Programing Guide :

If the delegate receives an NSStreamEvent.HasSpaceAvailable event and does not write anything to the stream, it does not receive further space-available events from the run loop until the NSOutputStream object receives more bytes. When this happens, the run loop is restarted for space-available events.

但是我没有数据可以再写入流。

我确实尝试向其中写入一些随机数据,但没有成功。我还尝试在 outputStream.hasSpaceAvailable 返回 true 后发送身份验证数据,但它也什么也没做。

我的代码有问题吗?

最佳答案

我终于意识到 stream() 函数不起作用只是因为该函数的签名不正确/已过时。

这是您正在使用的:

func stream(aStream: Stream, handleEvent eventCode: Stream.Event)

但实际上应该是:

func stream(_ aStream: Stream, handle eventCode: Stream.Event)

关于swift - 停留在 hasSpaceAvailable 并且无法从 NSInputStream 读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38882946/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com