小编典典

在socket.io的聊天室中获取多少人

node.js

我现在有这段代码设置了缺口和余地:

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;

阅读 89

收藏
2020-07-07

共1个答案

小编典典

如果您使用的版本<1,

var clients = io.sockets.clients(nick.room); // all users from room

2020-07-07