node.js 如何在 socket.io 1.0 中获取房间的客户列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23858604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get room's clients list in socket.io 1.0
提问by uzimith
I can get room's clients list with this code in socket.io 0.9.
我可以在 socket.io 0.9 中使用此代码获取房间的客户列表。
io.sockets.clients(roomName)
How can I do this in socket.io 1.0?
我怎样才能在 socket.io 1.0 中做到这一点?
回答by Michael
Consider this rather more complete answer linked in a comment above on the question: https://stackoverflow.com/a/24425207/1449799
考虑在上面关于这个问题的评论中链接的这个更完整的答案:https: //stackoverflow.com/a/24425207/1449799
The clients in a room can be found at
一个房间里的客户可以在
io.nsps[yourNamespace].adapter.rooms[roomName]
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
这是一个关联数组,键是套接字 ID。在我们的例子中,我们想知道一个房间里的客户数量,所以我们做了Object.keys(io.nsps[yourNamespace].adapter.rooms[roomName]).length
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 '/')
如果你还没有看到/使用过命名空间(比如这个家伙[我]),你可以在这里了解它们http://socket.io/docs/rooms-and-namespaces/ (重要的是:默认命名空间是 '/ ')
Updated (esp. for @Zettam):
更新(尤其是@Zettam):
checkout this repo to see this working: https://github.com/thegreatmichael/socket-io-clients
签出这个 repo 看看这个工作:https: //github.com/thegreatmichael/socket-io-clients
回答by nha
Using @ryan_Hdot link, I made a small temporary function in my code, which avoids maintaining a patch. Here it is :
使用@ryan_Hdot link,我在代码中创建了一个小的临时函数,以避免维护补丁。这里是 :
function getClient(roomId) {
var res = [],
room = io.sockets.adapter.rooms[roomId];
if (room) {
for (var id in room) {
res.push(io.sockets.adapter.nsp.connected[id]);
}
}
return res;
}
If using a namespace :
如果使用命名空间:
function getClient (ns, id) {
return io.nsps[ns].adapter.rooms[id]
}
Which I use as a temporary fix for io.sockets.clients(roomId)which becomes findClientsSocketByRoomId(roomId).
我用它作为临时修复,io.sockets.clients(roomId)它变成了findClientsSocketByRoomId(roomId).
EDIT :
编辑 :
Most of the time it is worth considering avoiding using this method if possible.
大多数情况下,如果可能的话,值得考虑避免使用这种方法。
What I do now is that I usually put a client in it's own room (ie. in a room whose name is it's clientID). I found the code more readable that way, and I don't have to rely on this workaround anymore.
我现在所做的是,我通常把一个客户放在它自己的房间里(即放在一个名字是它的 clientID 的房间里)。我发现这样的代码更具可读性,而且我不再需要依赖这种解决方法。
Also, I haven't tested this with a Redis adapter.
另外,我还没有用 Redis 适配器测试过这个。
If you have to, also see this related questionif you are using namespaces.
如果您必须使用命名空间,请参阅此相关问题。
回答by Depado
For those of you using namespaces I made a function too that can handle different namespaces. It's quite the same as the answer of nha.
对于那些使用命名空间的人,我也创建了一个可以处理不同命名空间的函数。和nha的回答完全一样。
function get_users_by_room(nsp, room) {
var users = []
for (var id in io.of(nsp).adapter.rooms[room]) {
users.push(io.of(nsp).adapter.nsp.connected[id]);
};
return users;
};
回答by Tony
As of at least 1.4.5 nha's method doesn't work anymore either, and there is still no public api for getting clients in a room. Here is what works for me.
至少从 1.4.5 开始,nha 的方法也不再起作用,并且仍然没有用于让客户进入房间的公共 api。这对我有用。
io.sockets.adapter.rooms[roomId]returns an object that has two properties, sockets, and length. The first is another object that has socketId's for keys, and boolean's as the values:
io.sockets.adapter.rooms[roomId]返回一个具有两个属性、套接字和长度的对象。第一个是另一个对象,它的键是 socketId,值是布尔值:
Room {
sockets:
{ '/#vQh0q0gVKgtLGIQGAAAB': true,
'/#p9Z7l6UeYwhBQkdoAAAD': true },
length: 2 }
So my code to get clients looks like this:
所以我获取客户端的代码如下所示:
var sioRoom = io.sockets.adapter.rooms[roomId];
if( sioRoom ) {
Object.keys(sioRoom.sockets).forEach( function(socketId){
console.log("sioRoom client socket Id: " + socketId );
});
}
回答by ryAn_Hdot
You can see this github pull requestfor discussion on the topic, however, it seems as though that functionality has been stripped from the 1.0 pre release candidate for SocketIO.
你可以看到这个 github pull request来讨论这个话题,但是,这个功能似乎已经从 SocketIO 的 1.0 预发布候选版本中剥离了。

