クライアントが退室したときに、ルームに接続しているユーザーの数を送信したいと考えています。
socket.leave()
遅れがあるようです。
それを適切に行う方法は?setTimeout() を使いたくない
socket.on('disconnect', function () {
var roomsToSayGoodbye = io.sockets.manager.roomClients[socket.id];
for (var room in roomsToSayGoodbye) {
io.sockets.in(room).clients().length; // For example: 6
socket.leave(room);
io.sockets.in(room).clients().length; // Still 6!!
// So, this is wrong -->
io.sockets.in(room).emit('nb-connections', { num: io.sockets.in(room).clients().length });
// <--
// and I need this to make it work, not clean! -->
setTimeout(function() {
io.sockets.in(room).clients().length; // Okay, now 5
}, 1000 );
}
}