I am trying to count the amount of users connecting to a room from a phone. However, I do not want to count all users connected to the room.
Javascript the phone loads:
socket.emit('mobilePlayer', {});
Server
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
I was never able to find a solution to counting specific users. However, I broke apart users viewing from mobile phones(Players) and users on desktops(spectators) in to different rooms then broadcasted events to the opposing room.
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});
});
});