套接字.io随机断开连接(v1.0.6)

Socket.io Random Disconnects (v1.0.6)

本文关键字:v1 连接 io 随机 断开 套接字      更新时间:2023-09-26

我使用的是最新版本的socket。使用Phaser和Node制作一个在线多人游戏。我的问题是,一旦客户端连接上,它们偶尔会随机断开连接。似乎没有一个具体的案例会发生这种情况。有时是在游戏完全空闲时,有时是在所有玩家都向服务器发送输入时。

通过检查socket的调试输出。在io中,我发现断开连接的原因是"ping超时"。具体来说,下面这行是从socket.js库文件中触发的:

Socket.prototype.setPingTimeout = function () {
  var self = this;
  clearTimeout(self.pingTimeoutTimer);
  self.pingTimeoutTimer = setTimeout(function () {
    self.onClose('ping timeout'); // <------------------------
  }, self.server.pingInterval + self.server.pingTimeout);
};

发生这种情况有什么原因吗?我只是在本地主机上测试我的服务器,所以我没有理由认为会有任何明显的延迟导致超时。我的套接字设置与套接字上的聊天应用程序示例一致。io的网站:

服务器:

//http server setup
var io = require('socket.io')(http);
io.on('connection', function(socket){
  //Game logic,socket listeners, io.emits
});
客户:

var socket = io();
//client side listeners, emissions back to server

我的问题是首先是什么可能的原因,我会得到ping超时间歇性?其次,我是否有办法将超时时间设置得更长/更短,以测试这如何影响我正在获得的断开连接的频率?

遗憾的是,您不能使用套接字修改ping间隔。如果你使用核心库(engine.io),


感谢paweowWszoła指出了正确的答案。根据socket.io的文档:

传递给套接字的相同选项。IO总是被传递给引擎。

所以你可以设置引擎的ping超时和间隔通过传递它们作为参数。

要求(socket . io)。listen(app, {pingTimeout: 4000, pingInterval: 4000});

你是在你的控制台得到这个吗,

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