简体   繁体   English

在socket.io中获取所有房间名称

[英]Get all room name in socket.io

im using socket.io v1.3.5 to create chat room, now i have a code: 即时通讯使用socket.io v1.3.5创建聊天室,现在我有一个代码:

console.log(socket.rooms);

It get output: 它得到输出:

[ 'pBtWJZqN23xAJw0sAAAE', 'test' ]
[ 'GPnwtq3gi9t1RZcCAAAD', 'Lobby' ]
[ 'n7tTlvoH1M7foT3ZAAAC', 'Lobby' ]

i had 3 socket.id in 2 room 'lobby' and 'test'. 我在2个房间“大厅”和“测试”中有3个socket.id。 I want to get an array of room name like this: 我想要一个这样的房间名称数组:

var listroom = ['Lobby','test']

Tks for read. Tks的阅读。

This is not a proper response: 这是不正确的回应:

[ 'pBtWJZqN23xAJw0sAAAE', 'test' ]
[ 'GPnwtq3gi9t1RZcCAAAD', 'Lobby' ]
[ 'n7tTlvoH1M7foT3ZAAAC', 'Lobby' ]

Let me guess it is actually: 让我猜测实际上是:

[['pBtWJZqN23xAJw0sAAAE', 'test'], ['GPnwtq3gi9t1RZcCAAAD', 'Lobby'], ['n7tTlvoH1M7foT3ZAAAC', 'Lobby']]

If it is not, then correct me. 如果不是,请纠正我。

Just iterate through the array and collect values: 只需遍历数组并收集值即可:

var values = [];
for (var i = 0; i < socket.rooms.length; i++)
{
    var room = socket.rooms[i];
    if (values.indexOf(room[1]) === -1) values.push(room[1]);
}

console.log(values);

Working JS Fiddle demo: http://jsfiddle.net/bcbctaLd/ 工作的JS Fiddle演示: http//jsfiddle.net/bcbctaLd/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM