node.js socket.io 获取当前套接字所在的房间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19044660/
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
socket.io get rooms which socket is currently in
提问by Herokiller
Is it possible to get rooms which socket is currently in, without calling
是否可以在不调用的情况下获取当前套接字所在的房间
io.sockets.clients(roomName)
for every room name and looking for this socket in results
对于每个房间名称并在结果中查找此套接字
采纳答案by Laurent Perrin
From the Socket.IO Room doc:
io.sockets.manager.roomClients[socket.id]
io.sockets.manager.roomClients[socket.id]
回答by Arjen
In socket.io version 1+ the syntax is:
在 socket.io 版本 1+ 中,语法是:
socket.rooms
回答by Alexandre Daubricourt
Cross-compatible way
交叉兼容方式
var rooms = Object.keys(io.sockets.adapter.sids[socket.id]);
// returns [socket.id, 'room-x'] or [socket.id, 'room-1', 'room-2', ..., 'room-x']
回答by natancodes
When using a non-default adapter, such as socket.io-redis, socket.roomsdoesn't seem to do the trick. The way I managed to get the rooms for a specific client without looping was to use io.sockets.adapter.sids[socket.id], which returns the rooms as an object.
使用非默认适配器(例如 socket.io-redis)时,socket.rooms似乎不起作用。我设法在不循环的情况下为特定客户端获取房间的方法是使用io.sockets.adapter.sids[socket.id],它将房间作为对象返回。
{ 'R-ZRgSf7h4wfPatcAAAC': true, ROOM: true, ROOM_2: true }
Note that this doesn't list sockets on other processes, though!
请注意,这不会列出其他进程上的套接字!
socket.io v1.3.7, socket.io-redis 1.0.0
socket.io v1.3.7,socket.io-redis 1.0.0
回答by Zing Lee
Version 1.7.3, socket.roomscontains socket.id, so remove it and get the list of rooms:
版本 1.7.3,socket.roomscontains socket.id,因此删除它并获取房间列表:
Object.keys(socket.rooms).filter(item => item!=socket.id);
In other version, you can print the socketand find the rooms.
在其他版本中,您可以打印socket并找到房间。
回答by StefansArya
Version 2.0.3
版本 2.0.3
io.sockets.sockets[yourSocketID].rooms
That equal with
那等于
socket.rooms
回答by Adry
Being sure that socket is in only one room at a time, my solution was:
确保插座一次只在一个房间里,我的解决方案是:
var currentRoom = Object.keys(io.sockets.adapter.sids[socket.id]).filter(item => item!=socket.id);
回答by FergusThePlant
Socket.io v2.1.1
Socket.io v2.1.1
So make sure you aren't accessing the sockets rooms in the disconnectevent like I was, as they have already left the rooms by the time that event is triggered. If you want to do that try it in the disconnectingevent - https://github.com/socketio/socket.io/pull/2332/files
因此,请确保您没有disconnect像我一样在事件中访问套接字房间,因为在触发事件时他们已经离开了房间。如果您想这样做,请在disconnecting活动中尝试- https://github.com/socketio/socket.io/pull/2332/files
Then you can use any of the following:
然后您可以使用以下任何一种:
Object.keys(socket.adapter.rooms)
Object.keys(socket.adapter.sids)
Object.keys(socket.rooms)
回答by pradeep
You can save room in socket itself when it joins a room
加入房间时,您可以在套接字本身中节省空间
// join room
socket.join(room);
// update socket's rooms
if (socket.rooms) {
socket.rooms.push(room);
} else {
socket.rooms = [room];
}
Later you can retrieve all rooms that the socket is in by simply
稍后您可以通过简单地检索套接字所在的所有房间
socket.rooms
From the Server API documentation:
从服务器 API 文档:
socket.rooms(object)
A hash of strings identifying the rooms this client is in, indexed by room name.
socket.rooms (object)
标识此客户端所在房间的字符串散列,按房间名称索引。
回答by filemono
socket.io 1.7.3 +
socket.io 1.7.3 +
var currentRoom = socket.rooms[Object.keys(socket.rooms)[0]];//returns name of room

