Socket.io如何处理客户端互联网断开事件? 我在stackoverflow上阅读heartbeat但该解决方案对我不起作用。
我找到了ping solution,但我认为ping所有连接用户是一种不好的方法,它会降低服务器速度
这是我的代码,请指导如何处理互联网断开事件?
#include<stdio.h>
void main()
{
char letter;
int num;
do
{
printf("Enter any number: ");
scanf("%d",&num);
printf("Square of %d is %d\n",num,num*num);
printf("you want to enter another no y/n ");
scanf("%c",&letter);
}while(letter=='y');
}
答案 0 :(得分:1)
// use this on server side
function sendHeartbeat(){
setTimeout(sendHeartbeat, 8000);
io.sockets.emit('ping', { beat : 1 });
}
io.sockets.on('connection', function (socket) {
socket.on('pong', function(data){
console.log("Pong received from client");
});
}
setTimeout(sendHeartbeat, 8000);
............................................... ..
// on Client side
socket.on('ping', function(data){
socket.emit('pong', {beat: 1});
});
希望这能帮到你