Socket.IO 获取连接的客户端列表
我花了很长时间查找有关 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是在客户端(在浏览器中)还是在服务器端执行此操作?您提到使用
alert
,但我不知道客户端上的clients()
方法可以执行您想要的操作。在服务器端,
clients()
返回一个Socket
数组 如此定义方法:Node.js 控制台和带有开发工具的浏览器提供了一个允许调试的
console
对象;一般来说,你可以调用console.log(something),其中something
几乎是任何东西;对于对象,将以一种或另一种方式枚举该对象的详细信息。[编辑]
由于您已经使用
socket.set
来存储用户名,也许您可以尝试这样的操作: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 aclients()
method on the client side that does what you want.On the server side,
clients()
returns an array ofSocket
s as defined by this method:The Node.js console and browsers with dev tools provide a
console
object that allows debugging; generally, you can callconsole.log(something)
wheresomething
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: