如何知道用户仍然在网站上在线还是离线?

问题描述:

我正在asp.net MVC网站上构建聊天系统,如果用户超过2分钟没有在该网站上执行任何操作,我会将其状态设置为离开".
我的问题是,如何知道用户是否处于离线状态,离线意味着他关闭了网站或退出了登录.

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.

我知道Membership类中有一个isOnline属性,但是我没有使用Membership在此网站上进行安全登录.

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

有没有办法知道用户是否在线,
或者如何实现这种成员身份.在我的代码中实现IsOnline以使其相同.

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.

要知道用户是否在站点上,您可以每隔一分钟从客户端向服务器发送AJAX请求,并在服务器端检查之间的时间间隔.收到要求查看他是否不再在页面上的请求.

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.

如果要检测用户是否为"AFK",但可能仍在浏览器中打开了网站,则必须为按键,鼠标按下和鼠标移动添加全局事件,并且仅发送AJAX如果在最近一分钟内触发了其中一个事件,请每分钟请求一次.

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.