计算房间内的手机用户



我正试图计算从电话连接到房间的用户数量。但是,我不想计算连接到房间的所有用户。

手机加载的Javascript:

socket.emit('mobilePlayer', {});
服务器

io.sockets.on('connection', function(socket){
    socket.on('mobilePlayer', function(data){
        socket.set('mobileClient', function(){
            console.log('mobile client connected and set');     
        })
    }); 
    socket.on('join', function (data, ball) {
        RoomModel.findById(data.room, 'title', function(err, room){
             socket.get('mobilePlayer', function (err) {
                 MobilePlayerCount =  io.sockets.clients('mobileClient').length; // Count mobile users 
                 console.log(MobilePlayerCount);  
            });
        }); // End RoomModel
    }); // End  Join
}); // End  connection

我一直无法找到计算特定用户数量的解决方案。然而,我将手机用户(玩家)和桌面用户(观众)分成不同的房间,然后将事件广播到对方房间。

io.sockets.on('connection', function(socket){
    socket.emit('connected', {message: 'Connected to NodePong!', from: "System"});
    socket.on('join', function (data, ball) {
        RoomModel.findById(data.room, 'title', function(err, room){
            if(!err && data.room){
                socket.join(room._id);
                console.log('joined');
                users++;
            }       
        clients = io.sockets.clients('5179546cf0eefca6f8000001').length; // count users in RoomModel    
            socket.emit('PlayerCount', { whichPlayer: clients});
            socket.on('paddleLocation', function(data){
                console.log('paddle on socket');
                socket.broadcast.emit('sendPaddledata', {data:data, whichPlayer: clients});
            }); 
        }); // End RoomModel
    }); // End  Join
    socket.on('leave', function (data) { 
        users--;
        console.log('leave'+ users);
        socket.leave();
    });
    socket.on('disconnect', function (data) { 
        users--;
        console.log('disconnect' + users);
        socket.broadcast.emit('userLeft', { whichPlayer: users});
    });

});

相关内容

  • 没有找到相关文章

最新更新