问题描述
我可以在 socket.io 0.9 中使用此代码获取房间的客户列表.
I can get room's clients list with this code in socket.io 0.9.
io.sockets.clients(roomName)
如何在 socket.io 1.0 中执行此操作?
How can I do this in socket.io 1.0?
推荐答案
请考虑以上问题评论中链接的更完整的答案:https://stackoverflow.com/a/24425207/1449799
可以在
io.nsps[yourNamespace].adapter.rooms[roomName]
这是一个关联数组,键是套接字 ID.在我们的例子中,我们想知道一个房间里的客户数量,所以我们做了 Object.keys(io.nsps[yourNamespace].adapter.rooms[roomName]).length
This is an associative array with keys that are socket ids. In our case, we wanted to know the number of clients in a room, so we did Object.keys(io.nsps[yourNamespace].adapter.rooms[roomName]).length
如果你没有见过/使用过命名空间(比如这个家伙[我]),你可以在这里了解它们http://socket.io/docs/rooms-and-namespaces/ (重要的是:默认命名空间是'/')
In case you haven't seen/used namespaces (like this guy[me]), you can learn about them here http://socket.io/docs/rooms-and-namespaces/ (importantly: the default namespace is '/')
更新(尤其是@Zettam):
Updated (esp. for @Zettam):
查看此存储库以查看此操作:https://github.com/thegreatmichael/socket-io-clients
checkout this repo to see this working: https://github.com/thegreatmichael/socket-io-clients
这篇关于如何在 socket.io 1.0 中获取房间的客户列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!