Reconnectingwebsocket is a small JavaScript library that encapsulates the WebSocket API, which provides a mechanism to automatically reconnect when a connection is broken.
Simply take the following:
WS = New WebSocket (' ws://... ');
Replace with:
WS = New Reconnectingwebsocket (' ws://... ');
See: Https://github.com/fc653074701/reconnecting-websocket
Or:
//interval sends the heartbeat packet data to the server, the server sends back the heartbeat packet response within a certain time, compares the time-out limit, if exceeds the set timeout time, thinks that the current WebSocket connection with the server has been disconnected, closes the current web socket connection, the aftercare process, for example, reconnect, Or pop-up tips ...functionkeepalive (ws) {varTime =NewDate (); if(Last_health! =-1 && (time.gettime)-last_health >health_timeout)) { //connection disconnected, can be set to reconnect or close connection$ ("#keeplive_box"). HTML ("The server is not responding.")). css ({"Color": "Red" }); //ws.close ();}Else { $("#keeplive_box"). HTML ("Connection OK"). css ({"Color": "Green" }); if(Ws.bufferedamount = = 0) {ws.send (' ~h#c~ '); } }}varWS =NewWebSocket (to_url); Ws.onopen=function () { $("#statustxt"). HTML ("connected.")); $("#send_btn"). attr ("Disabled",false); Heartbeat_timer= SetInterval (function() {keepalive (WS)},3000);}
Implementing a JavaScript library that WebSocket disconnects automatically