I am looking for a way to check the amount of users in a room to make sure that, if there are already 2 players in it, I can block the player from joining. However, the code and examples I have found online has been out-of-date. Currently, I am using socket.io 3.0.1.
io.on("connection", socket => {
socket.on("join", (room, username) => {
var users = io.sockets.clients(room)
if (typeof users === 'undefined') {
users = []
}
console.log(users.length);
if (users.length <= 1) {
socket.join(room);
io.in(room).emit("join", username);
socket.emit("lobbyJoined");
users = io.sockets.adapter.rooms[room]
console.log("users in " + room + ": " + users);
} else {
socket.emit("lobbyFull");
}
});
});
io.sockets.clients(room)
which is recommended in other posts, throws an error: TypeError: io.sockets.clients is not a function
, and io.sockets.adapter.rooms[room]
returns 'undefined', even if there is a user connected to that room.
Any help would be greatly appreciated.