我一直致力于与我的服务器建立连接,并已将其连接起来。但是没有调用流委托。我正在设置流委托给自己,尝试使用.schedule,使用.unretainedvalue以及.retainedvalue,已经铸造而不是将其转换为InputStream / OutputStream ..继承人我的乐趣connect()
var inputOpened = false
var outputOpened = false
var state: NetworkState?
var inputStream: InputStream!
var outputStream: OutputStream!
func connect() {
state = NetworkState.NetworkStateConnectingToServer
var readStream: Unmanaged<CFReadStream>?
var writeStream: Unmanaged<CFWriteStream>?
CFStreamCreatePairWithSocketToHost(nil, "192.168.1.4" as CFString, 1955, &readStream, &writeStream)
inputStream = readStream?.takeRetainedValue() as! InputStream!
outputStream = writeStream?.takeUnretainedValue() as! OutputStream!
inputStream.delegate = self
outputStream.delegate = self
inputStream.schedule(in: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode)
outputStream.schedule(in: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode)
inputStream.open()
outputStream.open()
}
和我的流委托功能
func stream(_ aStream: Stream, handle eventCode: Stream.Event) {
print("ran")
DispatchQueue.main.async(execute: {
if aStream == self.inputStream {
self.inputStreamHandle(eventCode)
}
else if aStream == self.outputStream {
self.outputStreamHandle(eventCode)
}
})
}