关于实施“在线状态”的建议。一个网站? [英] Advice on implementing "presence" for a web site?

查看:100
本文介绍了关于实施“在线状态”的建议。一个网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

理想情况下,我想找到一个简单,轻巧的代码,该代码使所有连接到我的网站的Web客户端都能实时了解其他在线用户的状态。

Ideally, I'd like to find simple, lightweight code that allows all the web clients connected to my site to maintain real-time status of who else is currently online.

我知道ejabberd可以做到这一点,但是它也可以做很多其他事情,我希望代码占用量少,以便我可以自定义并了解其性能特征。

I know ejabberd does this, but it also does a lot of other things, and I'd prefer a small code footprint so I can customize and understand its performance characteristics.

我喜欢node.js的非阻塞方面,并且想知道是否有一个开源项目可以执行所有这些逻辑。

I like the non-blocking aspect of node.js, and was wondering if there's an open source project that does all this logic.

我也想可以看到在客户端维护此模型的JavaScript实现。

I'd also like to see a JavaScript implementation of maintaining this model on the client side.

推荐答案

要获取实时状态,请使用 socket.io 。每次有人连接时,将他们添加到已连接的用户列表中。为了获得准确的统计信息,您需要跟踪用户会话。请参见 http://www.danielbaulig.de/socket-ioexpress/

For real time status, use socket.io. Every time someone connects add them to the connected users list. For accurate stats you will need to keep track of user sessions. See http://www.danielbaulig.de/socket-ioexpress/

var onlineUsers = {};
var online = 0;

io.sockets.on('connection', function (socket) {
  onlineUsers[socket.handshake.sessionID] = socket.handshake.session;
  online = Object.keys(onlineUsers).length;
  socket.broadcast.emit('online', online);

  socket.on('disconnect', function () {
    delete onlineUsers[socket.handshake.sessionID];
    online--;
    socket.broadcast.emit('online', online);
  });
});

这篇关于关于实施“在线状态”的建议。一个网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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