Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

socket-client-io for reconnection in js or javascript

var connected = false;
const RETRY_INTERVAL = 10000;
var timeout;

socket.on('connect', function() {
  connected = true;
  clearTimeout(timeout);
  socket.send({'subscribe': 'schaftenaar'});
  content.html("<b>Connected to server.</b>");
});

socket.on('disconnect', function() {
  connected = false;
  console.log('disconnected');
  content.html("<b>Disconnected! Trying to automatically to reconnect in " +                   
                RETRY_INTERVAL/1000 + " seconds.</b>");
  retryConnectOnFailure(RETRY_INTERVAL);
});

var retryConnectOnFailure = function(retryInMilliseconds) {
    setTimeout(function() {
      if (!connected) {
        $.get('/ping', function(data) {
          connected = true;
          window.location.href = unescape(window.location.pathname);
        });
        retryConnectOnFailure(retryInMilliseconds);
      }
    }, retryInMilliseconds);
  }

// start connection
socket.connect();
retryConnectOnFailure(RETRY_INTERVAL);
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript sleep 1 minute 
Javascript :: validar correo electronico en js 
Javascript :: to 2 decimal places javascript 
Javascript :: expressjs param 
Javascript :: string to object js 
Javascript :: upload image in firebase storage react web 
Javascript :: brain.js 
Javascript :: react sticky hook 
Javascript :: adding mui theme to index.js 
Javascript :: splice 
Javascript :: function hoisting in js 
Javascript :: set function to execute at certain time js 
Javascript :: loading page for all ajax call in jquery 3.3.1 
Javascript :: use the whatwg url api instead 
Javascript :: ckeditor ignore contenteditable 
Javascript :: javascript url 
Javascript :: toast notification angular bootstrap 8 
Javascript :: script src in folder 
Javascript :: export mongo to csv node 
Javascript :: make input bigger if text does not fit 
Javascript :: how to wait till jquery post request has been made 
Javascript :: express sendfile root path 
Javascript :: closure 
Javascript :: filter array js 
Javascript :: discord js invite to channel 
Javascript :: math module js 
Javascript :: s3.getobject nodejs example 
Javascript :: math.round 
Javascript :: array last element 
Javascript :: access an object js 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =