Implementing a JavaScript library that WebSocket disconnects automatically

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.