0

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

手机加载的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
4

1 回答 1

0

我从来没有找到计算特定用户的解决方案。但是,我将通过手机观看的用户(玩家)和在台式机上观看的用户(观众)分开到不同的房间,然后将事件广播到对面的房间。

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});
    });


});
于 2013-05-07T15:22:25.917 回答