gpt4 book ai didi

ios - 红蜘蛛的 socket.isConnected 总是返回 false

转载 作者:行者123 更新时间:2023-11-28 08:39:21 25 4
gpt4 key购买 nike

我正在使用 Swift 开发一个 iOS 应用程序,它通过套接字连接到 Android 设备。我已经实现了 Starscream 并且能够连接到其他设备。我的问题是,当我在 websocketDidConnect 委托(delegate)方法上打印某些内容时,它不会打印。此外,当我打印 socket.isConnected 时,它会打印 false。请帮助我。谢谢。也欢迎使用红蜘蛛的其他替代品,谢谢!

这是我的代码:

    import Starscream
class SettingsViewController: UIViewController, UITextFieldDelegate, WebSocketDelegate{

override func viewDidLoad() {
super.viewDidLoad()

let socket = WebSocket(url: NSURL(string: "ws://\(ipAddressTextField.text!):4000/")!)
socket.connect()

print("socket.isConnected \(socket.isConnected)") //this one is false

socket.onConnect = {
print("Connected...") // does not print
}

socket.onDisconnect = { (error: NSError?) in
print("websocket is disconnected: \(error?.localizedDescription)") // this one prints when I turn the wifi off
}

socket.onData = { (data: NSData) in
print("got some data: \(data.length)")

}

}

func websocketDidConnect(ws: WebSocket) {
print("websocket is connected")
}

func websocketDidDisconnect(ws: WebSocket, error: NSError?) {
if let e = error {
print("websocket is disconnected: \(e.localizedDescription)")
} else {
print("websocket disconnected")
}
}

func websocketDidReceiveMessage(ws: WebSocket, text: String) {
print("Received text: \(text)")
}

func websocketDidReceiveData(ws: WebSocket, data: NSData) {
print("Received data: \(data.length)")
}

}

最佳答案

您需要将其作为类属性,否则它将在退出 viewDidLoad 方法时被释放。

关于ios - 红蜘蛛的 socket.isConnected 总是返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36792175/

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