简体   繁体   中英

Get all room name in socket.io

im using socket.io v1.3.5 to create chat room, now i have a code:

console.log(socket.rooms);

It get output:

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

i had 3 socket.id in 2 room 'lobby' and 'test'. I want to get an array of room name like this:

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

Tks for read.

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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