Fixed a bug which causes the server to crash when a player that wasn'y in a room disconnected.
This commit is contained in:
parent
98e8ac05bb
commit
889d9a8065
@ -29,6 +29,7 @@ module.exports = {
|
|||||||
"semi": [
|
"semi": [
|
||||||
"error",
|
"error",
|
||||||
"always"
|
"always"
|
||||||
]
|
],
|
||||||
|
"no-mixed-spaces-and-tabs": "off"
|
||||||
}
|
}
|
||||||
};
|
};
|
36
src/index.js
36
src/index.js
@ -31,23 +31,27 @@ app.get("/", (req, res) => {
|
|||||||
io.on("connection", (socket) => {
|
io.on("connection", (socket) => {
|
||||||
// Handle disconnection
|
// Handle disconnection
|
||||||
socket.on("disconnect", () => {
|
socket.on("disconnect", () => {
|
||||||
const room = players[socket.id].room;
|
|
||||||
|
if (players[socket.id]) {
|
||||||
|
const room = players[socket.id].room;
|
||||||
|
|
||||||
|
var playerUpdate = {};
|
||||||
|
if (io.sockets.adapter.rooms[room]) {
|
||||||
|
for (const id in io.sockets.adapter.rooms[room].sockets) {
|
||||||
|
playerUpdate[id] = players[id];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
io.in(room).emit("roomUpdate", playerUpdate);
|
||||||
|
|
||||||
|
console.log(traffic.out + colours.disconnect(`User ${String(socket.id)} disconnected from room ${room}`));
|
||||||
|
|
||||||
|
if (io.sockets.adapter.rooms[room] == undefined) {
|
||||||
|
console.log(traffic.int + colours.room(`Room ${room} has been deleted due to a lack of players`));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
delete players[socket.id];
|
delete players[socket.id];
|
||||||
|
|
||||||
var playerUpdate = {};
|
|
||||||
if (io.sockets.adapter.rooms[room]) {
|
|
||||||
for (const id in io.sockets.adapter.rooms[room].sockets) {
|
|
||||||
playerUpdate[id] = players[id];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
io.in(room).emit("roomUpdate", playerUpdate);
|
|
||||||
|
|
||||||
console.log(traffic.out + colours.disconnect(`User ${String(socket.id)} disconnected from room ${room}`));
|
|
||||||
|
|
||||||
if (io.sockets.adapter.rooms[room] == undefined) {
|
|
||||||
console.log(traffic.int + colours.room(`Room ${room} has been deleted due to a lack of players`));
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Request identification
|
// Request identification
|
||||||
|
Loading…
Reference in New Issue
Block a user