红蜘蛛委托方法“didReceive”永远不会被叫到
我正在尝试使用 Starscream 4.0.4
连接到 websockets 服务器,但委托方法永远不会被调用。我尝试了很多不同的网址。当我调用 self.socket!.connect() 时,我没有收到任何错误消息,只是无提示的失败。我还尝试添加
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
到我的 Info.plist
中而不进行任何更改。我在这里做错了什么?
class WebSocketManager: WebSocketDelegate {
var socket: WebSocket? = nil
var isConnected: Bool = false
func connectToChat() {
print("connecting to chat...")
// I've tried many different URLs. The URLs work fine when tested elsewhere.
let chatUrlString = "wss://socketsbay.com/wss/v2/2/demo/"
var request = URLRequest(url: URL(string: chatUrlString)!)
request.timeoutInterval = 5
self.socket = WebSocket(request: request)
self.socket!.delegate = self
self.socket!.connect()
}
// This never gets called.
func didReceive(event: WebSocketEvent, client: WebSocket) {
print("websocket event fired!")
switch event {
case .connected(let headers):
self.isConnected = true
print("websocket is connected: \(headers)")
case .disconnected(let reason, let code):
self.isConnected = false
print("websocket is disconnected: \(reason) with code: \(code)")
case .text(let string):
print("Received text: \(string)")
case .binary(let data):
print("Received data: \(data.count)")
case .ping(_):
break
case .pong(_):
break
case .viabilityChanged(_):
break
case .reconnectSuggested(_):
break
case .cancelled:
self.isConnected = false
case .error(let error):
self.isConnected = false
self.handleError(error: error)
}
}
func handleError(error: Error?) {
print(error ?? "no error")
}
}
I'm trying to connect to a websockets server with Starscream 4.0.4
but the delegate method never gets called. I've tried many different urls. When I call self.socket!.connect()
, I get no error messages, just silent failure. I've also tried adding
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
to my Info.plist
with no change. What am I doing wrong here?
class WebSocketManager: WebSocketDelegate {
var socket: WebSocket? = nil
var isConnected: Bool = false
func connectToChat() {
print("connecting to chat...")
// I've tried many different URLs. The URLs work fine when tested elsewhere.
let chatUrlString = "wss://socketsbay.com/wss/v2/2/demo/"
var request = URLRequest(url: URL(string: chatUrlString)!)
request.timeoutInterval = 5
self.socket = WebSocket(request: request)
self.socket!.delegate = self
self.socket!.connect()
}
// This never gets called.
func didReceive(event: WebSocketEvent, client: WebSocket) {
print("websocket event fired!")
switch event {
case .connected(let headers):
self.isConnected = true
print("websocket is connected: \(headers)")
case .disconnected(let reason, let code):
self.isConnected = false
print("websocket is disconnected: \(reason) with code: \(code)")
case .text(let string):
print("Received text: \(string)")
case .binary(let data):
print("Received data: \(data.count)")
case .ping(_):
break
case .pong(_):
break
case .viabilityChanged(_):
break
case .reconnectSuggested(_):
break
case .cancelled:
self.isConnected = false
case .error(let error):
self.isConnected = false
self.handleError(error: error)
}
}
func handleError(error: Error?) {
print(error ?? "no error")
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论