告诉服务器该用户不再在互联网上 [英] Tell server that user is no more on internet
问题描述
我是node.js的新手,正在尝试使用node.js和socket.io进行群聊.我可以进行群聊,也可以管理系统中的数据.
I am new to node.js and trying to develop group chat using node.js and socket.io. I am able to do group chat also able to manage data in the system.
现在,我的问题是离线用户,即未连接到互联网. 我正在与该用户建立连接流,就好像我在做receiverUserSocket.emit("sendMsg",data)一样,无法验证此用户是否收到消息. 是的,有一个事件.on("disconnect"),但是我延迟了大约30-40秒.
Now, the problem with me is with offline users i.e not connected to internet. I am having connection stream of this user and as if I do receiverUserSocket.emit("sendMsg",data) there is no way to verify if this user received message or not. Yes, there is an event .on("disconnect") but I am getting delay of approx 30 - 40 seconds.
有什么方法可以识别我们要向其发送消息的用户是在线还是离线.
Is there any way we can identify that the user to whom we want to send message is online or offline.
推荐答案
最好的方法是使用socket.io心跳-假设您正在使用socket.io.
The best way is to use socket.io heartbeats - assuming you're using socket.io.
心跳和检测到终止连接的问题在这里得到了很好的解释:
Heartbeats and the problem with detecting a terminated connection are well explained here:
请记住,您可以控制心跳超时值(从而强制断开连接事件显示得更快),如此处所述:
Keep in mind that you can control heartbeat timeout values (thus forcing the disconnect event to appear much faster), as explained here:
但这可能会对您的服务器造成更大的压力.
But that might put much more strain on your server.
这篇关于告诉服务器该用户不再在互联网上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!