我正在尝试使用 socket.io 设置 node.js 服务器。我看到的问题是我的服务器每 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 秒断开+重新连接客户端?