Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SockJS Client auto reconnect?

I'm trying to figure out a way for my SockJS clients to reconnect to the server if it should go down.

I currently have this:

    new_conn = function() {    
        socket = new SockJS(protocol + serverDomain + '/echo', null, {
            'protocols_whitelist': ['websocket', 'xdr-streaming', 'xhr-streaming', 'iframe-eventsource', 'iframe-htmlfile', 'xdr-polling', 'xhr-polling', 'iframe-xhr-polling', 'jsonp-polling']
        });
    };

    socket.onopen = function () {
        clearInterval(recInterval);
    };  

    socket.onclose = function () {    
        recInterval = window.setInterval(function () {
            new_conn();
        }, 2000);
    }; 

The problem is that the setInterval keeps firing even after a successful reconnect. It seems that the socket.onopen never gets executed.

Any ideas what I could be doing wrong?

like image 929
Alosyius Avatar asked Sep 21 '13 08:09

Alosyius


2 Answers

I think it could be related to variable scoping. Try this:

var recInterval = null;

new_conn = function() {    
    socket = new SockJS(protocol + serverDomain + '/echo', null, {
        'protocols_whitelist': ['websocket', 'xdr-streaming', 'xhr-streaming', 'iframe-eventsource', 'iframe-htmlfile', 'xdr-polling', 'xhr-polling', 'iframe-xhr-polling', 'jsonp-polling']
    });
};

socket.onopen = function () {
    clearInterval(recInterval);
};  

socket.onclose = function () {    
    recInterval = window.setInterval(function () {
        new_conn();
    }, 2000);
}; 

Anyway, it's strange, because you were declaring recInterval on the window object, and it should have worked. If it doesn't work, you could also debug it with a browser, with debugger; statements or interactively by setting local breakpoints... (in onopen, for example).

By the way, I rewrited the whole code like this (I like refactoring:):

var recInterval = null;
var socket = null;

var new_conn = function() {    
    socket = new SockJS(protocol + serverDomain + '/echo', null, {
        'protocols_whitelist': ['websocket', 'xdr-streaming', 'xhr-streaming', 
                                'iframe-eventsource', 'iframe-htmlfile', 
                                'xdr-polling', 'xhr-polling', 'iframe-xhr-polling',
                                'jsonp-polling']
    });

    socket.onopen = function () {
        clearInterval(recInterval);
    };  

    socket.onclose = function () {    
        recInterval = setInterval(function () {
            new_conn();
        }, 2000);
    };
};
like image 136
franzlorenzon Avatar answered Nov 03 '22 10:11

franzlorenzon


In case anyone still interested in this topic: The refactored code snippet from franzlorenzon causes a lot of reconnects since it is kind of recursive reconnecting itself somehow since each two seconds a new onclose event is spawned (regardless of the recInterval).

Moving the clear interval right after the socket creation does the trick. I also added a socket = null in the onclose event.

var recInterval = null;
var socket = null;

var new_conn = function() {
  socket = new SockJS(protocol + serverDomain + '/echo', null, {
    'protocols_whitelist': ['websocket', 'xdr-streaming', 'xhr-streaming',
      'iframe-eventsource', 'iframe-htmlfile',
      'xdr-polling', 'xhr-polling', 'iframe-xhr-polling',
      'jsonp-polling'
    ]
  });

  clearInterval(recInterval);

  socket.onopen = function() {

  };

  socket.onclose = function() {
    socket = null;
    recInterval = setInterval(function() {
      new_conn();
    }, 2000);
  };
};
like image 37
daPhantom Avatar answered Nov 03 '22 09:11

daPhantom