如何使用红蜘蛛连接到webSocket?

时间:2018-01-20 07:50:09

标签: ios swift

我想建立套接字连接。我使用下面的代码

func websocketDidConnect(socket: WebSocketClient) {

    print("websocket is connected")
}

func websocketDidDisconnect(socket: WebSocketClient, error: Error?) {

    if let e = error as? WSError {

        print("websocket is disconnected: \(e.message)")

    } else if let e = error {

        print("websocket is disconnected: \(e.localizedDescription)")

    } else {

        print("websocket disconnected")
    }
}

func websocketDidReceiveMessage(socket: WebSocketClient, text: String) {

    print("Received text: \(text)")
}

func websocketDidReceiveData(socket: WebSocketClient, data: Data) {

    print("Received data: \(data.count)")
}

代表们

{{1}}

我收到此错误

websocket断开连接:HTTP升级无效

1 个答案:

答案 0 :(得分:2)

你的代表看起来还不错。在与websocket服务器协商连接协议参数时会出现问题。而不是制作URLRequest尝试这样的事情:

 let socket = WebSocket(url: URL(string: "wss://yourWebSocketServer")!)