webrtc-web: Not correctly checking number of clients in room (step-04)
In step-04/index.js the server checks how many clients are in the room by calling
var numClients = io.sockets.sockets.length;
log('Room ' + room + ' now has ' + numClients + ' client(s)');
However this returns undefined, as well as doesn’t actually try and check for the given room.
A proposed fix:
var clientsInRoom = io.nsps['/'].adapter.rooms[room];
var numClients = clientsInRoom === undefined ? 0 : Object.keys(clientsInRoom).length;
Or if you also want to upgrade to socket v1.4
var clientsInRoom = io.nsps['/'].adapter.rooms[room];
var numClients = clientsInRoom === undefined ? 0 : Object.keys(clientsInRoom.sockets).length;
A full fix + some small cleans up 😃 would look like
var clientsInRoom = io.nsps['/'].adapter.rooms[room];
var numClients = clientsInRoom === undefined ? 0 : Object.keys(clientsInRoom).
// socket.io 1.4.8
var numClients = clientsInRoom === undefined ? 0 : Object.keys(clientsInRoom.sockets).length;
// max two clients
if (numClients === 2) {
socket.emit('full', room);
return;
}
log('Room ' + room + ' now has ' + (numClients + 1) + ' client(s)');
if (numClients === 0) {
socket.join(room);
log('Client ID ' + socket.id + ' created room ' + room);
socket.emit('created', room, socket.id);
} else {
log('Client ID ' + socket.id + ' joined room ' + room);
io.sockets.in(room).emit('join', room);
socket.join(room);
socket.emit('joined', room, socket.id);
io.sockets.in(room).emit('ready');
}
See: https://github.com/AlexRobinson-/webrtc-web/blob/fix-check-room-clients/step-04/index.js
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 4
- Comments: 21 (17 by maintainers)
Commits related to this issue
- Correctly count number of clients in room In this patch, outdated code is fixed. Specifically, `var numClients = io.sockets.sockets.length;` does not work anymore and has therefore been updated. Thi... — committed to aaronang/webrtc-web by aaronang 7 years ago
- Correctly count number of clients in room In this patch, outdated code is fixed. Specifically, `var numClients = io.sockets.sockets.length;` does not work anymore and has therefore been updated. Thi... — committed to aaronang/webrtc-web by aaronang 7 years ago
@aaronang go for it!
@thbaja @nitobuendia Thank you!
I’ve published the changes to codelab text.
@thbaja Good catch, Thomas. Thank you. @samdutton Sent codelab text changes for review.
@samdutton This is fixed by #65
this solved the problem for me