您将如何实现“谁在线”?特征?

发布于 2024-08-22 16:04:58 字数 146 浏览 3 评论 0原文

我一直想在我的网站上实现“谁在线”功能。

想知道您如何确定用户是否在线?

一些选项是:

  1. 最后一次看到,不到 N 分钟前(N 是什么?)
  2. 具有长轮询的彗星服务器
  3. 其他东西

I have been meaning to implement a "who is online" feature on my site.

Was wondering how would you decide if a user is online or not?

Some options are:

  1. Last seen, less than N minutes ago (what is N?)
  2. A comet server with long polling
  3. Something else

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天涯离梦残月幽梦 2024-08-29 16:04:58

如果您使用会话变量,则如果 last_activity + session_expiry > 则用户在线。当前日期。否则会话已经过期并且他们不在线。

现在,这取决于人们能够使用“谁在线”功能做什么。您可能更喜欢更保守的措施,以便对用户的活跃度有更高的信心。

但是,考虑到网络的性质,没有可靠的方法可以确保用户真正在线并在您的网站中活跃,除非每隔一段时间就需要用户交互,但这会很烦人。

If you are using session variables then the user is online if last_activity + session_expiry > current_date. Else the session has already expired and they are not online.

Now, it depends on what people will be able to do with this "who is online" feature. You might prefer a more conservative measure to have a higher confidence the user's active.

But, given the nature of the web, there's no sure fire way to ensure the user's really online and active in your site, short of requiring user interaction every once in a while, but that would be annoying.

一口甜 2024-08-29 16:04:58

我会选择选项 1 并允许从配置文件中设置 N。据推测,用户活动在某些数据存储中被记录有时间戳,因此计算用户是否被视为在线(少于 N 次之前见过)应该非常简单。您可以考虑使用定期 AJAX 请求来定期更新在线用户集合。

I would go with option 1 and allow N to be set from a configuration file. Presumably user activity is being logged with a timestamp in some datastore, so calculating whether a user is considered online (seen less than N time ago) should be pretty straightforward. You may consider using a periodic AJAX request to update the online collection of users at regular intervals.

っ〆星空下的拥抱 2024-08-29 16:04:58

您还可以使用 ping 方法。大约每 30-60 秒从客户端向服务器发送一次轻型 Ajax 请求。保持请求和响应尽可能小以减少带宽,这应该几乎与 Comet 方法一样好。

You could also use a ping method. Send a light ajax request from the client to server approximately every 30-60 seconds. Keep the request and response as small as possible to reduce bandwidth and this should perform almost as well as the Comet method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文