2013-05-06 55 views
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 

回答

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


});