Starscream delegate method "didReceive" never gets called

656 views Asked by At

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")
    }
}
0

There are 0 answers