How to wait for a WebSocket's readyState to change

后端 未结 9 2347
梦如初夏
梦如初夏 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:08

    Your while loop is probably locking up your thread. Try using:

    setTimeout(function(){
        if(socket.readyState === 0) {
            //do nothing
        } else if (socket.readyState !=1) {
            //fallback
            setInterval(poll, interval);
        }
    }, 50);
    

提交回复
热议问题