检查用户是否在页面上 [英] Check if user is on page

查看:81
本文介绍了检查用户是否在页面上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个一个聊天应用程序,允许用户主机房。只是给了一些更多的反馈:

I'm building a one on one chat application that allows users to host rooms. Just to give a little more feedback:


  1. 用户创建房间(房间是公共的,任何人都可以查看,

  1. User creates room (rooms are public, anybody can view but if user hasn't signed up, chat will be disabled and user will be prompted to sign up for chat.)

其他用户注册了会议室

用户根据他们注册的订单与主持人聊天

Users chat with host based on order they signed up

喜欢显示主机是否在线。或者进一步,显示主机是否在房间里。

I would like to show if the host is online or not. Or to go one step further, show if the host is in the room.

我不知道该怎么做。

推荐答案

基本上,这项工作如下。

Basically, this things work as following.

用户采取行动(聊天刷新,消息发送,访问页面等)与用户相关联的令牌(例如,保存在服务器中的令牌,例如DB)以当前时间更新。

Every time user take actions (chat refresh, message sent, page visited, etc) a token associated to the user (token that is saved in your server, DB for example) is updated with current time.

| User ID     | Last action         |
-------------------------------------
| 14 (mark)   | 2012-02-09-10:34:53 |
| 56 (phil)   | 2012-02-09-09:12:34 |
|  9 (john)   | 2012-02-09-10:33:11 |

然后,您决定用户是否在线(5/10分钟?)

And then you decide an user is online if his token is not too old (5/10 minutes?)

如果您使用AJAX每隔1分钟向服务器发送一次定期请求,就可以改进此方法。此AJAX请求更新令牌值。所以,这样,你可以告诉用户是在线也是如果他什么也没有盯着页面。当他退出时(关闭浏览器或标签页),AJAX定期呼叫将停止运作,因此在5/10分钟后,使用者会宣告离线。

You can improve this method if you use AJAX to send a periodical request to server every 1 minute or so. This AJAX request update the token value. So, this way, you can tell the user is online also if he does nothing but stare at the page. And when he quit (close browser or tab), AJAX periodical call stops works, so after 5/10 minutes the user is declared offline.

祝你好运!

这篇关于检查用户是否在页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆