红蜘蛛委托方法“didReceive”永远不会被叫到

发布于 2025-01-20 04:27:11 字数 2068 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文