0

I have a socket.io server and a client runing correctly. Each time that the server is down, the client try to connect again each 5 seconds. When the server is up again they connect without problems.

But the problem comes when I wait long time before up the server again, when the server is up, it crashes showing :

info  - socket.io started
debug - client authorized
info  - handshake authorized DqN4t2YVP7NiqQi8zer9
debug - setting request GET /socket.io/1/websocket/DqN4t2YVP7NiqQi8zer9
debug - set heartbeat interval for client DqN4t2YVP7NiqQi8zer9
debug - client authorized for
debug - websocket writing 1::

buffer.js:287
  pool = new SlowBuffer(Buffer.poolSize);
         ^
RangeError: Maximum call stack size exceeded

Client reconnection (Executed each 5 seconds while is not connected):

function socket_connect() {
    if (!socket) {
        socket = io.connect('http://192.168.1.25:8088', { 'reconnect':false, 'connect timeout': 5000 });
    } else {
        socket.socket.connect();
    }
    socket.on("connect", function () {
        clearInterval(connect_interval);
        connect_interval = 0;
        socket.emit('player', { refresh_data:true });
    });
}

On server side, only with the socket instance, it crashes:

var io = require('socket.io').listen(8088);

I think that the problem is:

When the server goes up, it recive all the connections emited by the client each 5 seconds, (15 hours disconnected * 60 m * 60 s / 5 seconds reconnection) and it crashes.

What can i do to close the connections that the server try to do?

PS:If i reload the client, and after up the server, it works

romualdr
  • 184
  • 7

2 Answers2

1

The main idea for socket.io.js is to reuse an existing connection. You should only connect it once and then exchange messages by using socket.emit()

I am not sure why you are creating a new connection between your client and server for every 5 seconds. There is a limit on the number of connections the server can create, but that should be more than enough. If you put it in a loop then eventually the server will run out of sockets.

io.connect has to be executed once on the client, then may be you can socket.emit() every 5 seconds. Remove the { 'reconnect':false, 'connect timeout': 5000 } and you will be fine.

user568109
  • 47,225
  • 17
  • 99
  • 123
  • But I'm only executing a new connection when the server is down, when the server's up again, it's stop the callback of connect function. – javi_moralesf Feb 07 '13 at 09:48
  • If this is due to connections. Try this function socket_connect() { socket = io.connect('http://192.168.1.25:8088', {'reconnect':true, 'connect timeout': 5000 }); socket.on("connect", function () { clearInterval(connect_interval); connect_interval = 0; socket.emit('player', { refresh_data:true }); }); } give the output so that i can debug it – user568109 Feb 07 '13 at 11:24
0

I founded the problem...

Each time that the function socket_connect() is called, a "socket.on("connect" ..." function is created. So when the server turns up, a new connection is created, but the event "socket.on("connect" is fired multiple times...

The solution was:

function socket_connect() {
    if (!socket) {
        socket = io.connect('http://192.168.1.25:8088', { 'reconnect':false, 'connect timeout': 5000 });
    } else {
        socket.socket.connect();
    }
}

socket.on("connect", function () {
        clearInterval(connect_interval);
        connect_interval = 0;
        socket.emit('player', { refresh_data:true });
    });