【问题标题】:Counting users in room on mobile phone用手机统计房间内的用户
【发布时间】:2013-05-07 23:22:25
【问题描述】:

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

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

【问题讨论】:

    标签: javascript jquery node.js websocket


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多