admin管理员组

文章数量:1430297

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 pletely 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?

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 pletely 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?

Share Improve this question asked Aug 20, 2014 at 10:33 Craig InnesCraig Innes 1,57311 silver badges23 bronze badges 7
  • You have to debug it a little more. Could be that some player's websocket connection is getting killed, due to error in game logic. Error could be on server or client. If on client, server cannot reach it anymore. Debug the client side logic too. – user568109 Commented Aug 20, 2014 at 13:28
  • Try set localStorage.debug='*' before you load socket.io. I found my client was disconnecting as one of the handlers threw an error that socket.io catches internally and then re-connects for you without actually throwing the error to console. – Simeon Cheeseman Commented Aug 21, 2014 at 22:26
  • I am experiencing the same issue. Did you ever figure out an answer? – nathasm Commented Oct 30, 2014 at 19:54
  • Well, could you give us a working example? Or at least source code? – markasoftware Commented Nov 6, 2014 at 1:07
  • I'm experiencing a different issue, websocket connects but doesn't send data then after some attempts to send data, it flushes all data! any solution to this? – Super Hornet Commented Nov 9, 2014 at 6:29
 |  Show 2 more ments

2 Answers 2

Reset to default 1

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./Automattic/socket.io/wiki/Configuring-Socket.IO

本文标签: javascriptSocketio Random Disconnects (v106)Stack Overflow