Socket.IO 获取连接的客户端列表

发布于 2025-01-03 11:52:21 字数 1562 浏览 2 评论 0原文

我花了很长时间查找有关 io.sockets.clients() 的文档,以便可以检索连接的客户端 ID、用户名等。当我 alert(io.sockets. client().length.toString()); 它给了我正确的数字,但我需要找到数组的结构。有没有一种方法可以在不知道结构的情况下找出数组?很像 php 的 print_r() 吗?

到目前为止的代码:

socket.on('switchRoom', function(newroom){
    var clients = io.sockets.clients();
    socket.emit('users', clients);
});

socket.on('users', function(usernames) {
    for(var client in usernames) {
        console.log(usernames[client].id + ' disconnected: ' +     usernames[client].disconnected)
    }
});

在“switchRoom”被触发后我收到此错误

/home/ed/socket/chat/node_modules/socket.io/lib/parser.js:81
data = JSON.stringify(ev);
              ^
TypeError: Converting circular structure to JSON
at Object.stringify (native)
at Object.encodePacket (/home/ed/socket/chat/node_modules/socket.io/lib/parser.js:81:19)
at Socket.packet (/home/ed/socket/chat/node_modules/socket.io/lib/socket.js:202:21)
at Socket.emit (/home/ed/socket/chat/node_modules/socket.io/lib/socket.js:351:15)
at Socket.<anonymous> (/home/ed/socket/chat/app.js:58:10)
at Socket.$emit (events.js:64:17)
at SocketNamespace.handlePacket (/home/ed/socket/chat/node_modules/socket.io/lib/namespace.js:331:20)
at Manager.onClientMessage (/home/ed/socket/chat/node_modules/socket.io/lib/manager.js:436:38)
at WebSocket.onMessage (/home/ed/socket/chat/node_modules/socket.io/lib/transport.js:387:20)
at Parser.<anonymous> (/home/ed/socket/chat/node_modules/socket.io/lib/transports/websocket/hybi-07-12.js:38:10)

I'm having a heck of a time finding documentation on io.sockets.clients() so I can retrieve the connected clients id, username, etc. When I alert(io.sockets.clients().length.toString()); it gives me the correct number, but I need to find the structure of the array. Is there a way I can figure out the array without knowing the structure? Much like php's print_r()?

Code so far:

socket.on('switchRoom', function(newroom){
    var clients = io.sockets.clients();
    socket.emit('users', clients);
});

socket.on('users', function(usernames) {
    for(var client in usernames) {
        console.log(usernames[client].id + ' disconnected: ' +     usernames[client].disconnected)
    }
});

I receive this error after 'switchRoom' is fired

/home/ed/socket/chat/node_modules/socket.io/lib/parser.js:81
data = JSON.stringify(ev);
              ^
TypeError: Converting circular structure to JSON
at Object.stringify (native)
at Object.encodePacket (/home/ed/socket/chat/node_modules/socket.io/lib/parser.js:81:19)
at Socket.packet (/home/ed/socket/chat/node_modules/socket.io/lib/socket.js:202:21)
at Socket.emit (/home/ed/socket/chat/node_modules/socket.io/lib/socket.js:351:15)
at Socket.<anonymous> (/home/ed/socket/chat/app.js:58:10)
at Socket.$emit (events.js:64:17)
at SocketNamespace.handlePacket (/home/ed/socket/chat/node_modules/socket.io/lib/namespace.js:331:20)
at Manager.onClientMessage (/home/ed/socket/chat/node_modules/socket.io/lib/manager.js:436:38)
at WebSocket.onMessage (/home/ed/socket/chat/node_modules/socket.io/lib/transport.js:387:20)
at Parser.<anonymous> (/home/ed/socket/chat/node_modules/socket.io/lib/transports/websocket/hybi-07-12.js:38:10)

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

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

发布评论

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

评论(1

用心笑 2025-01-10 11:52:21

您是在客户端(在浏览器中)还是在服务器端执行此操作?您提到使用 alert,但我不知道客户端上的 clients() 方法可以执行您想要的操作。

在服务器端,clients() 返回一个 Socket 数组 如此定义方法

/**
 * Retrieves all clients as Socket instances as an array.
 *
 * @api public
 */

SocketNamespace.prototype.clients = function (room) {
  var room = this.name + (room !== undefined ?
     '/' + room : '');

  if (!this.manager.rooms[room]) {
    return [];
  }

  return this.manager.rooms[room].map(function (id) {
    return this.socket(id);
  }, this);
};

Node.js 控制台和带有开发工具的浏览器提供了一个允许调试的 console 对象;一般来说,你可以调用console.log(something),其中something几乎是任何东西;对于对象,将以一种或另一种方式枚举该对象的详细信息。

[编辑]

由于您已经使用 socket.set 来存储用户名,也许您可​​以尝试这样的操作:

// server

socket.on('connection', function(socket) {
  ...
  socket.on('disconnect', function() {
    socket.get('username', function (err, name) {
      socket.broadcast.emit('disconnected', name);
    });
  });
  ...
});

// client

socket.on('connect', function () {
  ...
  socket.on('disconnected', function(name) {
    console.log(name + " disconnected.");
  });
  ...
});

Are you doing this on the client side (in the browser) or on the server side? You mention using alert, but I'm not aware of a clients() method on the client side that does what you want.

On the server side, clients() returns an array of Sockets as defined by this method:

/**
 * Retrieves all clients as Socket instances as an array.
 *
 * @api public
 */

SocketNamespace.prototype.clients = function (room) {
  var room = this.name + (room !== undefined ?
     '/' + room : '');

  if (!this.manager.rooms[room]) {
    return [];
  }

  return this.manager.rooms[room].map(function (id) {
    return this.socket(id);
  }, this);
};

The Node.js console and browsers with dev tools provide a console object that allows debugging; generally, you can call console.log(something) where something pretty much anything; in the case of an object, the details of the object will be enumerated in one way or another.

[Edit]

Since you're already using socket.set to store the username, perhaps you can try something like this:

// server

socket.on('connection', function(socket) {
  ...
  socket.on('disconnect', function() {
    socket.get('username', function (err, name) {
      socket.broadcast.emit('disconnected', name);
    });
  });
  ...
});

// client

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