Javascript: js websocket disconnected reconnect library ReconnectingWebSocket

When websocket is connected, it is affected by the network

Or if the communication is not closed by the server for a long time, the disconnection reconnection mechanism is required

 

It is more troublesome to write the disconnection and reconnection by yourself, you can use this js library ReconnectingWebSocket.js

 https://github.com/joewalnes/reconnecting-websocket/ Download the min file directly, just import it

When using, only need to replace h5’s native websocket with ReconnectingWebSocket, everything else remains the same

For example: this is the use of this in vue.socket is the global ReconnectingWebSocket object, and other callback functions are also defined on the vue method

            the this .socket = new new ReconnectingWebSocket ( " xxxxxx " ); // Create Socket instance 
            the this .socket.debug = to true ;
             the this .socket.timeoutInterval = 10000 ; // connection timeout 
            the this .socket.reconnectInterval = 5000 ; // reconnection Interval time 
            this .socket.maxReconnectInterval = 600000 ; // Maximum reconnect interval time 
            this .socket.maxReconnectAttempts = 10 ; // Maximum number of reconnect attempts 
            this .socket.onmessage = this.OnMessage;
             this .socket.onopen = this .OnOpen;
             this .socket.onerror = this .OnError;
             this .socket.onclose = this .OnClose;

 

 If there is no communication for more than one minute, it will be interrupted, and then automatically reconnect

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *