node.js sockets.io断开每25秒(心跳相关)

我试图设置一个node.js服务器,使用socket.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,0.9.1 / 0.9.1-1吗? 如果是这样,这是吉列尔莫确认的一个已知问题。 目前的build议是回滚到0.9.0或0.8.7

https://github.com/LearnBoost/socket.io/issues/777

你不需要发送心跳来保持套接字的连接,也是socket.emit的语法

  socket.emit('event_name',{"data"});