我试图设置一个node.js服务器,使用socket.io。我看到的问题是我的服务器每隔25秒就会断开连接并重新连接客户端。node.js sockets.io断开每25秒(心跳相关)
这里是我的服务器代码:
var io = require('socket.io').listen(5876);
io.sockets.on('connection', function (socket)
{
console.log("New connection established");
socket.on('disconnect', function()
{
console.log("A client has disconnected.");
});
}
我的客户端连接使用socket.io.js分布。据我了解(显然不正确),我需要我的客户端每15秒发送一次“心跳”(消息“:: 2”),以防止服务器认为连接已中断并断开连接。基本上:
<script src="socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:5876');
socket.on('connect', function(data)
{
console.log("Connected to server.");
setInterval(function() { socket.emit("::2"); console.log("sent heartbeat"); }, 15000); // schedule a heartbeat for every 15 seconds
});
</script>
但客户端仍然断开连接+每25秒重新连接(不包括第一个第25秒)。
Node.js的服务器控制台日志看起来是这样的(可能已经切出一些早期的相同的连接/断开阶段,因为它是每25秒呼应):
New connection established
debug - emitting heartbeat for client 652791160849839915
debug - websocket writing 2::
debug - set heartbeat timeout for client 652791160849839915
debug - got heartbeat packet
debug - cleared heartbeat timeout for client 652791160849839915
debug - set heartbeat interval for client 652791160849839915
info - transport end
debug - set close timeout for client 652791160849839915
debug - cleared close timeout for client 652791160849839915
debug - cleared heartbeat interval for client 652791160849839915
A client has disconnected.
debug - discarding transport
debug - client authorized
info - handshake authorized 5961067041159055274
debug - setting request GET /socket.io/1/websocket/5961067041159055274
debug - set heartbeat interval for client 5961067041159055274
debug - client authorized for
debug - websocket writing 1::
New connection established
我怎么能阻止我从服务器每隔25秒断开连接并重新连接客户端?
据我知道你不需要因为这是由socket.io处理手动发送心跳包。尝试在取出手动心跳呼叫后检查连接和断开连接事件。 – f0x 2012-03-05 10:06:01
你有没有解决这个问题? \t 我有确切的问题。我降级到0.8.7,仍然是同样的问题。调试 - 为客户端启动心跳超时3744936361854682925信息 - 传输端调试 - 为客户端设置关闭超时3744936361854682925调试 - 为客户端清除关闭超时3744936361854682925 – 2016-09-11 19:31:46
@PranoyC这里有一个解决方法可能工作:https://github.com/socketio/socket .o/issues/777除此之外,我最近才刚刚使用最新版本的socket.io,并没有遇到任何问题。 – Josh1billion 2016-09-11 21:52:44