关于实施“存在”的建议对于一个网站?

发布于 2024-12-23 17:23:38 字数 233 浏览 2 评论 0原文

理想情况下,我希望找到简单、轻量级的代码,允许连接到我的网站的所有 Web 客户端维护当前在线的其他人的实时状态。

我知道 ejabberd 可以做到这一点,但它还可以做很多其他事情,而且我更喜欢小的代码占用空间,这样我就可以定制和了解它的性能特征。

我喜欢 Node.js 的非阻塞特性,并且想知道是否有一个开源项目可以完成所有这些逻辑。

我还希望看到在客户端维护此模型的 JavaScript 实现。

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.

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.

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

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

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

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

发布评论

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

评论(2

风吹雪碎 2024-12-30 17:23:38

如需实时状态,请使用 socket.io。每次有人连接时,将他们添加到已连接的用户列表中。为了获得准确的统计数据,您需要跟踪用户会话。请参阅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);
  });
});

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);
  });
});
浸婚纱 2024-12-30 17:23:38

对于任何将来阅读本文的人。我从 fent 写的答案开始,但我需要一些修改,因为自他的答案发布以来的 6 年里,情况发生了变化。

我使用 Map 而不是 Object 来存储会话信息。这意味着不再需要 sessionID

const onlineUsers = new Map();

io.on('connection', function (socket) {
    console.log('connection initiated');

    socket.on('userInfo', userInfo => {
        console.log('userInfo', userInfo);

        onlineUsers.set(socket, userInfo);

        io.emit('onlineUsers', Array.from(onlineUsers.values()));
    });

    socket.on('disconnect', function () {
        console.log(onlineUsers.get(socket));

        onlineUsers.delete(socket);

        io.emit('onlineUsers', Array.from(onlineUsers.values()));
    });
});

For anyone reading this in the future. I started with the answer written by fent, but I needed some modifications since things have changed in the intervening 6 years since his answer was published.

I used a Map rather than an Object for storing the session information. this means that the sessionID is no longer required.

const onlineUsers = new Map();

io.on('connection', function (socket) {
    console.log('connection initiated');

    socket.on('userInfo', userInfo => {
        console.log('userInfo', userInfo);

        onlineUsers.set(socket, userInfo);

        io.emit('onlineUsers', Array.from(onlineUsers.values()));
    });

    socket.on('disconnect', function () {
        console.log(onlineUsers.get(socket));

        onlineUsers.delete(socket);

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