This question already has answers here:
Socket.IO - how do I get a list of connected sockets/clients?

(27个答案)


去年关闭。




我现在有这段代码设置了缺口和余地:
io.sockets.on('connection', function(client){
    var Room = "";
    client.on("setNickAndRoom", function(nick, fn){
        client.join(nick.room);
        Room = nick.room;
        client.broadcast.to(Room).emit('count', "Connected:" + " " + count);
        fn({msg :"Connected:" + " " + count});
    });

我想知道我如何才能有多少人连接到特定的聊天室...例如Room.length

客户端 :
function Chat(){
    this.socket = null;
    this.Nickname = "";
    this.Room = "";
    var synched = $('#syncUp');
    this.Connect = function(nick, room){
        socket =  io.connect('http://vybeing.com:8080');
        Nickname = nick;
        Room = room;
        //conectarse
        socket.on('connect',function (data) {
            socket.emit('setNickAndRoom', {nick: nick, room: room}, function(response){
                $("#connection").html("<p>" + response.msg + "</p>");
            });
        });
}

我发现了这个,但是它给出了未定义的:
count = io.rooms[Room].length;

最佳答案

如果您使用的版本var clients = io.sockets.clients(nick.room); // all users from room

关于javascript - 在socket.io的聊天室中获取多少人,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9352549/

10-10 15:26