告诉服务器用户不在互联网上 [英] 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 heartbeats - 假设您使用的是 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屋!