0

I have the following reconnect method for Sockjs which almost is fully working:

(function() {
  // Initialize the socket & handlers
  var connectToServer = function() {
    var warbleSocket = new SockJS('http://url.com:5555/warble');

    warbleSocket.onopen = function() {
      clearInterval(connectRetry);
      $('.connect-status')
        .removeClass('disconnected')
        .addClass('connected')
        .text('Connected');
    };

    warbleSocket.onmessage = function(e) {
      $('#warble-msg').text(e.data);
    };

    warbleSocket.onclose = function() {
      clearInterval(connectRetry);
      connectRetry = setInterval(connectToServer, 1000);
      $('.connect-status')
        .removeClass('connected')
        .addClass('disconnected')
        .text('Disconnected');
    };

    // Connect the text field to the socket
    $('.msg-sender').off('input').on('input', function() {
      warbleSocket.send($('.msg-sender input').val()); 
    });

    function send(a) {
        warbleSocket.send(a);    
    }

    return {
        send: send
    };
  }();
  var connectRetry = setInterval(connectToServer, 1000);
})();

The error i am getting is when its trying to reconnect. Error is:

SyntaxError: missing ] after element list

at this line:

connectRetry = setInterval(connectToServer, 1000);

Any ideas what im doing wrong here?

JJJ
  • 32,902
  • 20
  • 89
  • 102
Alosyius
  • 8,771
  • 26
  • 76
  • 120
  • 1
    The error is caused by something outside this code. You have neither an element list here, nor an opening `[`. – Brian Oct 05 '13 at 14:21
  • why do you have `}();` before `var connectRetry = setInterval(connectToServer, 1000);`. Not sure if that is causing issue. It should have been just `};` – Rajesh Oct 05 '13 at 14:23
  • @Rajesh without the `}();` i wont be able to use `connectToServer.send("Hello")` outside of the `connectToServer` – Alosyius Oct 05 '13 at 14:27
  • Actually if i remove the `()` and do `};` it works, but then `connectToServer.send("Hello")` stops to work. – Alosyius Oct 05 '13 at 14:27
  • The point of `connectRetry = setInterval(connectToServer, 1000);` inside the `onclose` is to re-run connectToServer so it tried to reconnect – Alosyius Oct 05 '13 at 14:55

2 Answers2

1

Your connectToServer variable is not a function, it's an object with a property send that is a function, so it doesn't make sense to say setInterval(connectToServer, 1000). Try this instead:

setInterval(connectToServer.send, 1000);
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
0

Why don't you simplify things a bit?

I would put connection stuff inside a specific function and call it from setInterval().

Something like this (use with care since I'm not testing this code, ok?):

(function() {
  // Initialize the socket & handlers
  var connectToServer = function() {
    var warbleSocket;

    function connect() { 
      warbleSocket = new SockJS('http://url.com:5555/warble');

      warbleSocket.onopen = function() {
        // ...
      };

      warbleSocket.onmessage = function(e) {
        // ... 
      };

      warbleSocket.onclose = function() {
        // ...
    }

    // Connect the text field to the socket
    $('.msg-sender').off('input').on('input', function() {
      warbleSocket.send($('.msg-sender input').val()); 
    });

    function send(a) {
        warbleSocket.send(a);    
    }

    return {
        send: send
    };
  }();

  // you probably will need to call the first connection
  connectToServer();

  // and than set connection retry
  var connectRetry = setInterval(connectToServer.connect, 1000);
})();

I hope it helps you.

Regards,

Heleno

helenov
  • 341
  • 1
  • 2
  • 15