Skip to content

Commit

Permalink
socket.io v4.0.1 update
Browse files Browse the repository at this point in the history
 - added disconnect event to io
  • Loading branch information
halilcakar committed Apr 13, 2021
1 parent 5418722 commit deee1ac
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
"dotenv": "8.2.0",
"ejs": "3.1.3",
"express": "4.17.1",
"socket.io": "3.0.5"
"socket.io": "^4.0.1"
}
}
22 changes: 22 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,28 @@ io.on("connection", (socket) => {
}
}
});

socket.on("disconnect", () => {
let channel = channels.find((ch) =>
[ch.creatorID, ch.visitorID].includes(socket.id)
);
if (channel) {
if (channel.creatorID === socket.id) {
channel.creator = null;
channel.creatorID = null;
}
if (channel.visitorID === socket.id) {
channel.visitor = null;
channel.visitorID = null;
}
io.in(channel.id).emit("userLeft");

if (channel.visitor === null && channel.creator === null) {
channels.splice(channels.indexOf(channel), 1);
process.nextTick(() => (channel = null));
}
}
});
});

server.listen(PORT, () => {
Expand Down

0 comments on commit deee1ac

Please sign in to comment.