How to wait for a WebSocket's readyState to change

后端 未结 9 2359
梦如初夏
梦如初夏 2020-11-30 00:54

I\'m trying to implement a WebSocket with a fallback to polling. If the WebSocket connection succeeds, readyState becomes 1, but if it fails, readyState

9条回答
  •  北荒
    北荒 (楼主)
    2020-11-30 01:13

    Just like you defined an onmessage handler, you can also define an onerror handler. This one will be called when the connection fails.

    var socket = new WebSocket(url);
    socket.onmessage = onmsg;
    socket.onerror = function(error) {
        // connection failed - try polling
    }
    

提交回复
热议问题