after progress on the question how to create socket.io multicast groups, I found making rooms a great way to do what I needed.
在关于如何创建socket.io多播组的问题上取得进展之后,我发现制作房间是一种很好的方式来完成我需要的工作。
However, it would be great to know about all the rooms, without an extra data-structure.
但是,如果没有额外的数据结构,了解所有房间会很棒。
Is it possible to get a list of all rooms on the server from the server socket?
是否可以从服务器套接字获取服务器上所有房间的列表?
7 个解决方案
#1
31
The short answer:
简短的回答:
io.sockets.adapter.rooms
I analysed io
:
我分析了io:
I got the following output:
我得到以下输出:
{ server:
{ stack:
[ [Object],
[Object],
[Object],
[Object],
[Object],
[Object] ],
connections: 3,
allowHalfOpen: true,
watcher: { host: [Circular], callback: [Function] },
_events:
{ request: [Function],
connection: [Function: connectionListener],
listening: [Object],
upgrade: [Object] },
httpAllowHalfOpen: false,
cache: {},
settings: { home: '/', env: 'development', hints: true },
redirects: {},
isCallbacks: {},
_locals: { settings: [Object], app: [Circular] },
dynamicViewHelpers: {},
errorHandlers: [],
route: '/',
routes:
{ app: [Circular],
routes: [Object],
params: {},
_params: [],
middleware: [Function] },
router: [Getter],
__usedRouter: true,
type: 'tcp4',
fd: 7 },
namespaces:
{ '':
{ manager: [Circular],
name: '',
sockets: [Object],
auth: false,
flags: [Object],
_events: [Object] } },
sockets:
{ manager: [Circular],
name: '',
sockets: { '210837319844898486': [Object] },
auth: false,
flags: { endpoint: '', exceptions: [] },
_events: { connection: [Function] } },
settings:
{ origins: '*:*',
log: true,
store:
{ options: undefined,
clients: [Object],
manager: [Circular] },
logger: { colors: true, level: 1 },
heartbeats: true,
resource: '/socket.io',
transports:
[ 'websocket',
'htmlfile',
'xhr-polling',
'jsonp-polling' ],
authorization: [Function],
'log level': 1,
'close timeout': 25,
'heartbeat timeout': 15,
'heartbeat interval': 20,
'polling duration': 20,
'flash policy server': true,
'flash policy port': 843,
'destroy upgrade': true,
'browser client': true,
'browser client minification': false,
'browser client etag': false,
'browser client handler': false,
'client store expiration': 15 },
handshaken:
{ '210837319844898486':
{ headers: [Object],
address: [Object],
time: 'Mon Jul 18 2011 00:53:27 GMT+0200 (CEST)',
xdomain: false,
secure: undefined,
PHPSESSID: '7qo6cht3q0rskhfes4eesb2d05' } },
connected: { '210837319844898486': true },
open: { '210837319844898486': true },
closed: {},
closedA: [],
rooms:
{ '': [ '210837319844898486' ],
'/public-alfred': [ '210837319844898486' ] },
roomClients: { '210837319844898486': [ '': true, '/public-alfred': true ] },
oldListeners: [ [Function] ],
_events:
{ 'set:origins': [Function],
'set:flash policy port': [Function],
'set:transports': [Function] } }
after joining room "public-alfred" from a single client io.sockets.adapter.rooms
contained:
从一个客户io.sockets.adapter.rooms加入房间“public-alfred”后,包含:
{ '': [ '210837319844898486' ],
'/public-alfred': [ '210837319844898486' ] }
#2
16
In a new version of socket.io (1.x), io.sockets.manager.rooms
will cause an error. You should use io.sockets.adapter.rooms
instead.
在socket.io(1.x)的新版本中,io.sockets.manager.rooms将导致错误。你应该使用io.sockets.adapter.rooms。
#3
11
As everyone said, in a new version of socket.io (1.x) the rooms can be found at:
正如大家所说,在socket.io(1.x)的新版本中,房间可以在以下位置找到:
io.sockets.adapter.rooms
This will return something like:
这将返回如下内容:
{
'qNADgg3CCxESDLm5AAAA': [ 'qNADgg3CCxESDLm5AAAA': true ],
'myRoom': [ 'qNADgg3CCxESDLm5AAAA': true,
'0rCX3v4pufWvQ6uwAAAB': true,
'iH0wJHGh-qKPRd2RAAAC': true ],
'0rCX3v4pufWvQ6uwAAAB': [ '0rCX3v4pufWvQ6uwAAAB': true ],
'iH0wJHGh-qKPRd2RAAAC': [ 'iH0wJHGh-qKPRd2RAAAC': true ]
}
The only room that I want to get is 'myRoom', so I wrote the following function for doing that:
我想要的唯一房间是'myRoom',所以我写了以下函数:
function findRooms() {
var availableRooms = [];
var rooms = io.sockets.adapter.rooms;
if (rooms) {
for (var room in rooms) {
if (!rooms[room].hasOwnProperty(room)) {
availableRooms.push(room);
}
}
}
return availableRooms;
}
This was kind of confusing for me, hope this helps!
这对我来说有点困惑,希望这有帮助!
#4
1
I was able to find them using socket._events on an open connection.
我能够在打开的连接上使用socket._events找到它们。
io.sockets.on('connection', function(socket){
socket.on('ping', function(data){
console.log(socket._events);
});
});
Output:
输出:
{
ping: [Function],
disconnect: [Function]
}
#5
1
If io.sockets.adapter
is undefined
..
I'm using socket.io 1.7.1 (Server).
我正在使用socket.io 1.7.1(服务器)。
It seems io.of("......").sockets.adapter
is undefined
on that version of socket.io
. So I used io.adapter.rooms
instead and it works.
似乎io.of(“......”)。套接字.adapter在该版本的socket.io上未定义。所以我使用了io.adapter.rooms而且它有效。
#6
1
in case you are using
如果你正在使用
socket.io 2.1.0
socket.io 2.1.0
and chanced upon this.
并偶然发现这一点。
in 2.1.0, all the rooms in the server are located in the variable io.sockets.adapter.rooms
在2.1.0中,服务器中的所有房间都位于变量io.sockets.adapter.rooms中
//Use Example//
sockets.on('getRooms', function() {
sockets.emit('rooms', io.sockets.adapter.rooms);
});
You will get the following results.
您将获得以下结果。
{ xqfGuWYkKzT9hGgGAAAB: Room { sockets: { xqfGuWYkKzT9hGgGAAAB: true }, length: 1 }, '0Ak9nTjme7bLsD8NAAAC': Room { sockets: { '0Ak9nTjme7bLsD8NAAAC': true }, length: 1 } }
{xqfGuWYkKzT9hGgGAAAB:客房{插口:{xqfGuWYkKzT9hGgGAAAB:真},长度:1}, '0Ak9nTjme7bLsD8NAAAC':客房{插口:{ '0Ak9nTjme7bLsD8NAAAC':真},长度:1}}
#7
0
I don't have enough reputation to post my comment to the original question, but in addition, if you want to list all the rooms in a namespace, it should be coded like this:
我没有足够的声誉将我的评论发布到原始问题,但另外,如果要列出命名空间中的所有房间,它应该像这样编码:
var nsp = io.of('/my-nsp');
var rooms = nsp.adapter.rooms;
This would help so that if a client join a room by:
如果客户通过以下方式加入房间,这将有所帮助:
socket.join('my-room');
You should be able to see your room name when you do console.log(rooms);
当你做console.log(房间)时,你应该能够看到你的房间名称。
I just want to answer an unanswered comment above as I ran into the same issue after I saw mismatched room names.
我只想回答上面未回答的评论,因为在看到不匹配的房间名称后我遇到了同样的问题。
#1
31
The short answer:
简短的回答:
io.sockets.adapter.rooms
I analysed io
:
我分析了io:
I got the following output:
我得到以下输出:
{ server:
{ stack:
[ [Object],
[Object],
[Object],
[Object],
[Object],
[Object] ],
connections: 3,
allowHalfOpen: true,
watcher: { host: [Circular], callback: [Function] },
_events:
{ request: [Function],
connection: [Function: connectionListener],
listening: [Object],
upgrade: [Object] },
httpAllowHalfOpen: false,
cache: {},
settings: { home: '/', env: 'development', hints: true },
redirects: {},
isCallbacks: {},
_locals: { settings: [Object], app: [Circular] },
dynamicViewHelpers: {},
errorHandlers: [],
route: '/',
routes:
{ app: [Circular],
routes: [Object],
params: {},
_params: [],
middleware: [Function] },
router: [Getter],
__usedRouter: true,
type: 'tcp4',
fd: 7 },
namespaces:
{ '':
{ manager: [Circular],
name: '',
sockets: [Object],
auth: false,
flags: [Object],
_events: [Object] } },
sockets:
{ manager: [Circular],
name: '',
sockets: { '210837319844898486': [Object] },
auth: false,
flags: { endpoint: '', exceptions: [] },
_events: { connection: [Function] } },
settings:
{ origins: '*:*',
log: true,
store:
{ options: undefined,
clients: [Object],
manager: [Circular] },
logger: { colors: true, level: 1 },
heartbeats: true,
resource: '/socket.io',
transports:
[ 'websocket',
'htmlfile',
'xhr-polling',
'jsonp-polling' ],
authorization: [Function],
'log level': 1,
'close timeout': 25,
'heartbeat timeout': 15,
'heartbeat interval': 20,
'polling duration': 20,
'flash policy server': true,
'flash policy port': 843,
'destroy upgrade': true,
'browser client': true,
'browser client minification': false,
'browser client etag': false,
'browser client handler': false,
'client store expiration': 15 },
handshaken:
{ '210837319844898486':
{ headers: [Object],
address: [Object],
time: 'Mon Jul 18 2011 00:53:27 GMT+0200 (CEST)',
xdomain: false,
secure: undefined,
PHPSESSID: '7qo6cht3q0rskhfes4eesb2d05' } },
connected: { '210837319844898486': true },
open: { '210837319844898486': true },
closed: {},
closedA: [],
rooms:
{ '': [ '210837319844898486' ],
'/public-alfred': [ '210837319844898486' ] },
roomClients: { '210837319844898486': [ '': true, '/public-alfred': true ] },
oldListeners: [ [Function] ],
_events:
{ 'set:origins': [Function],
'set:flash policy port': [Function],
'set:transports': [Function] } }
after joining room "public-alfred" from a single client io.sockets.adapter.rooms
contained:
从一个客户io.sockets.adapter.rooms加入房间“public-alfred”后,包含:
{ '': [ '210837319844898486' ],
'/public-alfred': [ '210837319844898486' ] }
#2
16
In a new version of socket.io (1.x), io.sockets.manager.rooms
will cause an error. You should use io.sockets.adapter.rooms
instead.
在socket.io(1.x)的新版本中,io.sockets.manager.rooms将导致错误。你应该使用io.sockets.adapter.rooms。
#3
11
As everyone said, in a new version of socket.io (1.x) the rooms can be found at:
正如大家所说,在socket.io(1.x)的新版本中,房间可以在以下位置找到:
io.sockets.adapter.rooms
This will return something like:
这将返回如下内容:
{
'qNADgg3CCxESDLm5AAAA': [ 'qNADgg3CCxESDLm5AAAA': true ],
'myRoom': [ 'qNADgg3CCxESDLm5AAAA': true,
'0rCX3v4pufWvQ6uwAAAB': true,
'iH0wJHGh-qKPRd2RAAAC': true ],
'0rCX3v4pufWvQ6uwAAAB': [ '0rCX3v4pufWvQ6uwAAAB': true ],
'iH0wJHGh-qKPRd2RAAAC': [ 'iH0wJHGh-qKPRd2RAAAC': true ]
}
The only room that I want to get is 'myRoom', so I wrote the following function for doing that:
我想要的唯一房间是'myRoom',所以我写了以下函数:
function findRooms() {
var availableRooms = [];
var rooms = io.sockets.adapter.rooms;
if (rooms) {
for (var room in rooms) {
if (!rooms[room].hasOwnProperty(room)) {
availableRooms.push(room);
}
}
}
return availableRooms;
}
This was kind of confusing for me, hope this helps!
这对我来说有点困惑,希望这有帮助!
#4
1
I was able to find them using socket._events on an open connection.
我能够在打开的连接上使用socket._events找到它们。
io.sockets.on('connection', function(socket){
socket.on('ping', function(data){
console.log(socket._events);
});
});
Output:
输出:
{
ping: [Function],
disconnect: [Function]
}
#5
1
If io.sockets.adapter
is undefined
..
I'm using socket.io 1.7.1 (Server).
我正在使用socket.io 1.7.1(服务器)。
It seems io.of("......").sockets.adapter
is undefined
on that version of socket.io
. So I used io.adapter.rooms
instead and it works.
似乎io.of(“......”)。套接字.adapter在该版本的socket.io上未定义。所以我使用了io.adapter.rooms而且它有效。
#6
1
in case you are using
如果你正在使用
socket.io 2.1.0
socket.io 2.1.0
and chanced upon this.
并偶然发现这一点。
in 2.1.0, all the rooms in the server are located in the variable io.sockets.adapter.rooms
在2.1.0中,服务器中的所有房间都位于变量io.sockets.adapter.rooms中
//Use Example//
sockets.on('getRooms', function() {
sockets.emit('rooms', io.sockets.adapter.rooms);
});
You will get the following results.
您将获得以下结果。
{ xqfGuWYkKzT9hGgGAAAB: Room { sockets: { xqfGuWYkKzT9hGgGAAAB: true }, length: 1 }, '0Ak9nTjme7bLsD8NAAAC': Room { sockets: { '0Ak9nTjme7bLsD8NAAAC': true }, length: 1 } }
{xqfGuWYkKzT9hGgGAAAB:客房{插口:{xqfGuWYkKzT9hGgGAAAB:真},长度:1}, '0Ak9nTjme7bLsD8NAAAC':客房{插口:{ '0Ak9nTjme7bLsD8NAAAC':真},长度:1}}
#7
0
I don't have enough reputation to post my comment to the original question, but in addition, if you want to list all the rooms in a namespace, it should be coded like this:
我没有足够的声誉将我的评论发布到原始问题,但另外,如果要列出命名空间中的所有房间,它应该像这样编码:
var nsp = io.of('/my-nsp');
var rooms = nsp.adapter.rooms;
This would help so that if a client join a room by:
如果客户通过以下方式加入房间,这将有所帮助:
socket.join('my-room');
You should be able to see your room name when you do console.log(rooms);
当你做console.log(房间)时,你应该能够看到你的房间名称。
I just want to answer an unanswered comment above as I ran into the same issue after I saw mismatched room names.
我只想回答上面未回答的评论,因为在看到不匹配的房间名称后我遇到了同样的问题。