这是一个重复的问题,虽然我想问一下。 我想要所有用户名和&有socket.id谁当前连接到一个房间,如何得到它,
我尝试过: var clients = io.sockets.clients('room1');
这是我的代码ref
// when the client emits 'adduser', this listens and executes
socket.on('adduser', function(username){
// store the username in the socket session for this client
socket.username = username;
// store the room name in the socket session for this client
// socket.myroom = 'room1';
// add the client's username to the global list
usernames[username] = username;
// send client to room 1
socket.join('room1');
socket.join('room2');
// echo to client they've connected
socket.emit('updatechat', 'room1', 'you have connected to room1');
socket.emit('updatechat', 'room2', 'you have connected to room2');
// echo to room 1 that a person has connected to their room
socket.broadcast.to('room1').emit('updatechat', 'room1', username + ' has connected to this room');
socket.broadcast.to('room2').emit('updatechat', 'room2', username + ' has connected to this room');
var clients = io.sockets.clients('room1');
console.log('socket.id :'+ socket.id);
console.log('projson :'+ clients);
console.log('projson :'+ clients.socket.id);
console.log('projson :'+ clients.socket);
});
这是日志
debug: set heartbeat interval for client lhF85vZH16MLuMxyuIFH
debug: client authorized for
debug: websocket writing 1::
debug: websocket writing 5:::{"name":"updatechat","args":["room1","you have connected to room1"]}
debug: websocket writing 5:::{"name":"updatechat","args":["room2","you have connected to room2"]}
debug: broadcasting packet
debug: websocket writing 5:::{"name":"updatechat","args":["room1","p3 has connected to this room"]}
debug: broadcasting packet
debug: websocket writing 5:::{"name":"updatechat","args":["room2","p3 has connected to this room"]}
socket.id :lhF85vZH16MLuMxyuIFH
**projson :[object Object],[object Object]**
projson :undefined
projson :undefined
如何获得socketid&用户名,任何帮助将不胜感激, 谢谢
答案 0 :(得分:1)
尝试
console.log(io.sockets.manager.rooms)
这会给你这样的东西:
{
"room1":[socketidOfUser1,socketidOfUser2],
"room2":[socketidOfUser3,socketidOfUser4],
}
看看这是否提供了您需要的信息
答案 1 :(得分:0)
获取套接字ID:
Object.keys(io.nsps['/'].adapter.rooms[roomName].sockets)