I am using the latest version of socket.io (1.0.6) to make an online multiplayer game with Phaser and Node. My problem is that once the clients have connected, they will occasionally and at random, disconnect. There does not seem to be a specific case in which this happens. Sometimes it is while the game is completely idle, other times it is while all players are sending inputs to the server.
By checking the debug output from socket.io, I found that the reason for the disconnects is a "ping timeout". Specifically, the following line is being fired from the socket.js library file:
Socket.prototype.setPingTimeout = function () {
var self = this;
clearTimeout(self.pingTimeoutTimer);
self.pingTimeoutTimer = setTimeout(function () {
self.onClose('ping timeout'); // <------------------------
}, self.server.pingInterval + self.server.pingTimeout);
};
Is there a reason this would be happening? I am just testing my server over localhost, so I have no reason to think there would be any significant delay to cause a timeout. My sockets are set up in line with the chat app example on socket.io's website:
Server:
//http server setup
var io = require('socket.io')(http);
io.on('connection', function(socket){
//Game logic,socket listeners, io.emits
});
Client:
var socket = io();
//client side listeners, emissions back to server
My question is firstly what are the possible reasons I would be getting a ping timeout intermittently? and secondly, is there any way for me to set the timeout time much longer / shorter to test out how this affects the frequency of disconnects I am getting?
Unfortunately you can't modify the ping intervals using socket.io, you could if you used the core library (engine.io).
Thanks to Paweł Wszoła for pointing out the correct answer. According to the docs on socket.io:
The same options passed to socket.io are always passed to the engine.io Server that gets created.
So you can set engine's ping timeout and interval by passing them as parameters.
require('socket.io').listen(app, { pingTimeout: 4000, pingInterval: 4000 });
Are you getting this in ur console,
debug - setting request GET /socket.io/1/jsonp-polling/487577450665437510?t=1312872393095&i=1
debug - setting poll timeout
debug - clearing poll timeout
debug - jsonppolling writing io.j[1]("7:::1+0");
debug - set close timeout for client 487577450665437510
warn - client not handshaken client should reconnect
info - transport end
debug - cleared close timeout for client 487577450665437510
debug - discarding transport
debug - setting request GET /socket.io/1/xhr-polling/487577450665437510
debug - setting poll timeout
debug - clearing poll timeout
debug - xhr-polling writing 7:::1+0
debug - set close timeout for client 487577450665437510
warn - client not handshaken client should reconnect
info - transport end
debug - cleared close timeout for client 487577450665437510
debug - discarding transport
debug - setting request GET /socket.io/1/jsonp-polling/487577450665437510?t=1312872393150&i=1
debug - setting poll timeout
debug - clearing poll timeout
debug - jsonppolling writing io.j[1]("7:::1+0");
debug - set close timeout for client 487577450665437510
warn - client not handshaken client should reconnect
info - transport end
debug - cleared close timeout for client 487577450665437510
debug - discarding transport
debug - setting request GET /socket.io/1/xhr-polling/487577450665437510
debug - setting poll timeout
debug - clearing poll timeout
debug - xhr-polling writing 7:::1+0
debug - set close timeout for client 487577450665437510
warn - client not handshaken client should reconnect
https://github.com/Automattic/socket.io/wiki/Configuring-Socket.IO