我想在客户离开时发送连接到房间的用户数量。
好像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 );
}
}