How to know if the user is still online on the website or offline?

梦想的初衷 提交于 2019-12-01 08:27:53

问题


I am building a chat system in an asp.net MVC website, if the user has no actions on the website for more than 2 minutes I will set his status to away.
My question is, how to know if the user is offline, offline means he closed the website or signer out.

I knew that there is an isOnline property in the Membership classes but I am not using Membership for secure login in this website.

Is there a way to know if the user is online or not,
Or how this membership.IsOnline is implemented to make the same in my code.


回答1:


To know if the user is on the site, you could send an AJAX request every minute from the client to the server and server-side check the time between the received requests to see if he is no longer on the page.

If you want to the detect if the user is "AFK" but might still have the website opened on his browser, you'll have to add global events for key presses, mouse presses and mouse move, and only send the AJAX request each minute if there has been one of those event fired in the last minute.



来源:https://stackoverflow.com/questions/6903081/how-to-know-if-the-user-is-still-online-on-the-website-or-offline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!