Skip to content

Commit

Permalink
Clean up follow on disconnecting.
Browse files Browse the repository at this point in the history
  • Loading branch information
barnabasmolnar committed Aug 6, 2023
1 parent 8df0a73 commit 0718ec5
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,26 @@ try {
}
});

// TODO follow-mode unfollow on disconnect?
socket.on("disconnecting", async () => {
socketDebug(`${socket.id} has disconnected`);
for (const roomID in socket.rooms) {
const otherClients = (await io.in(roomID).fetchSockets()).filter(
(_socket) => _socket.id !== socket.id,
);

if (otherClients.length > 0) {
const isFollowRoom = roomID.startsWith("follow_");

if (!isFollowRoom && otherClients.length > 0) {
socket.broadcast.to(roomID).emit(
"room-user-change",
otherClients.map((socket) => socket.id),
);
}

if (isFollowRoom && otherClients.length === 0) {
const clientId = roomID.replace("follow_", "");
io.to(clientId).emit("broadcast-unfollow");
}
}
});

Expand Down

0 comments on commit 0718ec5

Please sign in to comment.