发送ping pong消息来检查客户端是否连接到服务器?
问题描述:
我正在使用Websockets,是否需要在连接到套接字时分别发送ping pong消息以检查客户端是否处于活动状态,或者Websockets是否有其内置机制来检查客户端是否连接到它。发送ping pong消息来检查客户端是否连接到服务器?
答
如果您使用的是“socket.io”程序包,当客户端断开服务器时会触发“断开”事件......所以您不需要定期检查以确定客户端是否联机或离线。
检查下面的代码(如果你使用socket.io):
io.sockets.on('connection', function(socket) { //on connect event
//handling other events ...
socket.on('disconnect', function() { //on disconnect event
console.log('Got disconnect!');
});
});
,如果您使用的网络套接字访问链接@robertklep建议
希望它可以帮助你...
https://github.com/websockets/ws#how-to-detect-and-close-broken-connections – robertklep