如何检测用户的连接丢失或他关闭Nodejs socket.io中的浏览器窗口

时间:2022-07-17 19:40:49

I have a chat application on Node.js and Socket.io, user can connect and disconnect with a button... I am having a list of online users which is perfectly managed with the help of my defined events that user trigger.

But the problem is I am unable to detect if the user has lost his connection or closed the browser window without disconnecting himself manually(by the disconnect button)...

我在Node.js和Socket.io上有一个聊天应用程序,用户可以通过按钮连接和断开连接...我有一个在线用户列表,可以通过用户触发的已定义事件进行完美管理。但问题是我无法检测用户是否丢失了连接或关闭了浏览器窗口而没有手动断开连接(通过断开连接按钮)...

This socket.io event is fired only when user disconnects himself not when he lost his connection.

仅当用户断开连接时才会触发此socket.io事件。

socket.on('disconnect',function(){
   console.log('user disconnected');  });

I want some really good mechanism to keep an eye on users in order to update my Online users list.

我想要一些非常好的机制来关注用户以更新我的在线用户列表。

1 个解决方案

#1


15  

And what's the difference? Closing the window, cutting the ethernet wire... it's all the same for the server: end of connection.

有什么区别?关闭窗口,切断以太网线......对于服务器来说都是一样的:连接结束。

Reading the socket.io docs: https://github.com/LearnBoost/socket.io/wiki/Exposed-events

阅读socket.io文档:https://github.com/LearnBoost/socket.io/wiki/Exposed-events

socket.on('disconnect', function() {}) - the disconnect event is fired in all cases, when the client-server connection is closed. It fires on wanted, unwanted, mobile, unmobile, client and server disconnects.

socket.on('disconnect',function(){}) - 当客户端 - 服务器连接关闭时,在所有情况下都会触发disconnect事件。它会触发通缉,不需要的,移动,不移动,客户端和服务器断开连接。

You should not rely your button, as people can disconnect without using that button. Use the disconnect event and once a socket disconnects (a socket, not a user, cause Node just knows about sockets) you will have to find out who was the "owner" of that socket and flag him as "disconnected". Or even better, wait for a few seconds and then flag him as offline. Why? Because the disconnect event will trigger even if the user just reloads the page, or navigates to another one. So if you wait a few seconds the user will be online again.

您不应该依赖按钮,因为人们可以在不使用该按钮的情况下断开连接。使用disconnect事件,一旦套接字断开连接(套接字,而不是用户,导致Node只知道套接字),你将不得不找出谁是该套接字的“所有者”并将其标记为“已断开连接”。或者甚至更好,等待几秒钟然后将他标记为离线。为什么?因为即使用户只是重新加载页面,也会触发disconnect事件,或者导航到另一个页面。因此,如果您等待几秒钟,用户将再次在线。

I had this problem too, and I ended up creating a "watcher" that runs every X seconds and flags users as offline when they don't own any socket or when they seem to be away (no activity for a long time).

我也遇到了这个问题,我最终创建了一个每隔X秒运行一次的“观察者”,当用户没有任何套接字或者他们似乎不在时(长时间没有活动)时,将用户标记为离线。

#1


15  

And what's the difference? Closing the window, cutting the ethernet wire... it's all the same for the server: end of connection.

有什么区别?关闭窗口,切断以太网线......对于服务器来说都是一样的:连接结束。

Reading the socket.io docs: https://github.com/LearnBoost/socket.io/wiki/Exposed-events

阅读socket.io文档:https://github.com/LearnBoost/socket.io/wiki/Exposed-events

socket.on('disconnect', function() {}) - the disconnect event is fired in all cases, when the client-server connection is closed. It fires on wanted, unwanted, mobile, unmobile, client and server disconnects.

socket.on('disconnect',function(){}) - 当客户端 - 服务器连接关闭时,在所有情况下都会触发disconnect事件。它会触发通缉,不需要的,移动,不移动,客户端和服务器断开连接。

You should not rely your button, as people can disconnect without using that button. Use the disconnect event and once a socket disconnects (a socket, not a user, cause Node just knows about sockets) you will have to find out who was the "owner" of that socket and flag him as "disconnected". Or even better, wait for a few seconds and then flag him as offline. Why? Because the disconnect event will trigger even if the user just reloads the page, or navigates to another one. So if you wait a few seconds the user will be online again.

您不应该依赖按钮,因为人们可以在不使用该按钮的情况下断开连接。使用disconnect事件,一旦套接字断开连接(套接字,而不是用户,导致Node只知道套接字),你将不得不找出谁是该套接字的“所有者”并将其标记为“已断开连接”。或者甚至更好,等待几秒钟然后将他标记为离线。为什么?因为即使用户只是重新加载页面,也会触发disconnect事件,或者导航到另一个页面。因此,如果您等待几秒钟,用户将再次在线。

I had this problem too, and I ended up creating a "watcher" that runs every X seconds and flags users as offline when they don't own any socket or when they seem to be away (no activity for a long time).

我也遇到了这个问题,我最终创建了一个每隔X秒运行一次的“观察者”,当用户没有任何套接字或者他们似乎不在时(长时间没有活动)时,将用户标记为离线。