Handling connection loss with websockets

前端 未结 4 1836
清酒与你
清酒与你 2020-12-07 10:32

I\'ve recently set-up a local WebSocket server which works fine, however I\'m having a few troubles understanding how I should handle a sudden loss of connection which neith

4条回答
  •  旧时难觅i
    2020-12-07 10:48

    The websocket protocol defines control frames for ping and pong. So basically, if the server sends a ping, the browser will answer with a pong, and it should work also the other way around. Probably the WebSocket server you use implements them, and you can define a timeout in which the browser must responds or be considered dead. This should be transparent for your implementation in both browser and server.

    You can use them to detect half open connections: http://blog.stephencleary.com/2009/05/detection-of-half-open-dropped.html

    Also relevant: WebSockets ping/pong, why not TCP keepalive?

提交回复
热议问题