简体   繁体   English

使用NowJS / Node.js实时跟踪在线/离线状态?

[英]Keeping track of online/offline status in real-time with NowJS/Node.js?

I've got a site where users login and are connected to NowJS on all pages. 我有一个网站,用户可以登录并在所有页面上连接到NowJS。 I'd like to be able to keep track of their status (online/offline) in near-realtime, but I'm not sure the best way to do this. 我希望能够实时跟踪其状态(在线/离线),但是我不确定做到这一点的最佳方法。 My initial thought was to just use the connect and disconnect events, but this won't work because those get fired each time the user goes to a new page (since the socket connection is closed while the new page loads). 我最初的想法是仅使用connect和断开连接事件,但这将不起作用,因为每次用户进入新页面时都会触发这些事件(因为套接字加载是在新页面加载时关闭的)。

I've got it somewhat working where I record a timestamp in the database entry for that user on the disconnect event, and then when the connect event fires I check if they've accessed the site within the last 30 seconds. 我已经在某种程度上起作用了,我在断开事件上为该用户记录了一个数据库条目中的时间戳记,然后当连接事件触发时,我检查了他们是否在最近30秒内访问了该站点。 If they haven't, I can assume they are starting a new session. 如果还没有,我可以假设他们正在开始新的会话。 On the disconnect event I use setTimeout to see if they are still online 30 seconds from now, and then set their status to offline if not. 在断开连接事件上,我使用setTimeout来查看它们从现在开始30秒钟是否仍处于联机状态,然后将它们的状态设置为“脱机”。

While this mostly works, it seems somewhat hacky and has several edge case issues that could be problematic (not to mention the fact that your going to have a new timeout server-side on each page view). 尽管这大部分可行,但似乎有些hacker,并且存在一些可能会引起问题的极端情况问题(更不用说您将在每个页面视图上拥有一个新的超时服务器端的事实)。 Is there a better way to do this that I'm just overlooking? 有什么更好的方法可以做到这一点,而我只是忽略了?

The right way to do this depends on more than just 'whether there is an open connection'. 正确的方法不仅取决于“是否有开放的连接”。 Are you trying to display to other users whether someone is around/available for chat/whatever? 您是否要向其他用户显示某人是否在附近/可以聊天/什么? Then you might also consider an inactivity timeout on the page itself (to capture the times when people walk away from their computer without closing the page). 然后,您还可以考虑页面本身的不活动超时(以捕获人们离开计算机而没有关闭页面的时间)。 If you're doing something else, like metering usage, you might want a different mechanism. 如果您要执行其他操作(例如计量使用),则可能需要其他机制。

I think the solution here is extending the timeout option for Socket.IO (NowJS uses this internally) or use your hacky solution with setTimeout. 我认为这里的解决方案是扩展Socket.IO的超时选项(NowJS在内部使用此选项),或者将您的hacky解决方案与setTimeout结合使用。

Honestly that seems the best solution considering users can move to another page quickly. 老实说,这似乎是考虑到用户可以快速移至另一个页面的最佳解决方案。

What I ended up doing was creating a global object that holds the persistent ID for the user (so in this case their ObjectId in MongoDB) as the key and the reference to the setTimeout as the value. 我最后要做的是创建一个全局对象,该对象持有用户的永久ID(因此,在本例中为MongoDB中的ObjectId)作为键,并引用setTimeout作为值。 I then do clearTimeout on a 'connect' event and delete the key from the object when they are considered offline. 然后,我对'connect'事件执行clearTimeout,并在将它们视为脱机时从对象中删除密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM