套接字.IO:如何在node.js中获得所有连接的用户到特定的房间



这是一个重复的问题,虽然我想问它。我想要所有的用户名&套接字。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 &用户名,任何帮助将不胜感激,谢谢你

try

console.log(io.sockets.manager.rooms)

这将给你像这样的东西:

{
    "room1":[socketidOfUser1,socketidOfUser2],
    "room2":[socketidOfUser3,socketidOfUser4],
}

查看是否提供了您需要的信息

获取套接字id:

Object.keys(io.nsps['/'].adapter.rooms[roomName].sockets)

相关内容

  • 没有找到相关文章

最新更新