我可以使用它,这是我的代码:确保在某处保留连接类的引用:-)
class Connection : NSObject, NSStreamDelegate { let serverAddress: CFString = "127.0.0.1" let serverPort: UInt32 = 8443 private var inputStream: NSInputStream! private var outputStream: NSOutputStream! func connect() { println("connecting...") var readStream: Unmanaged<CFReadStream>? var writeStream: Unmanaged<CFWriteStream>? CFStreamCreatePairWithSocketToHost(nil, self.serverAddress, self.serverPort, &readStream, &writeStream) // documentation suggests readStream and writeStream can be assumed to // be non-nil. If you believe otherwise, you can test if either is nil // and implement whatever error-handling you wish. self.inputStream = readStream!.takeRetainedValue() self.outputStream = writeStream!.takeRetainedValue() self.inputStream.delegate = self self.outputStream.delegate = self self.inputStream.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode) self.outputStream.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode) self.inputStream.open() self.outputStream.open() } func stream(stream: NSStream, handleEvent eventCode: NSStreamEvent) { println("stream event") }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)