How to wait for a WebSocket's readyState to change

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

    Look on http://dev.w3.org/html5/websockets/

    Search for "Event handler" and find the Table.

    onopen -> open
    onmessage -> message
    onerror ->error
    onclose ->close

    function update(e){ /*Do Something*/};
    var ws = new WebSocket("ws://localhost:9999/");
    
    ws.onmessage = update;
    

提交回复
热议问题