Skip to content

Commit

Permalink
attached roomId to socket and add some logging message
Browse files Browse the repository at this point in the history
  • Loading branch information
whitesnowx committed Nov 1, 2024
1 parent 8ff3220 commit 78510be
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions backend/collaboration-service/handler/socketHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ const handleSocketIO = (io) => {
socket.on("createSocketRoom", async ({ data, id, currentUser }) => {
// Store the socket id for the user
socketMap[currentUser] = socket.id;
socket.roomId = id;

socket.join(id);
console.log(`User with socket ID ${socket.id} joined room with ID ${id}`);
if (activeUserInRoom[id]) {
activeUserInRoom[id] = 1
} else {
activeUserInRoom[id] = activeUserInRoom[id] + 1
} else {
activeUserInRoom[id] = 1
}
console.log(`UactiveUserInRoom[id]: ${activeUserInRoom[id]}`);

const room = io.sockets.adapter.rooms.get(id);

Expand Down Expand Up @@ -71,15 +73,15 @@ const handleSocketIO = (io) => {
if (haveNewData[id]) {
haveNewData[id] = false;
await collabRef.update(periodicData);
console.log(`Collab Data updated to Firebase at ${currentTime}`);
console.log(`Collab Data for roomid ${id} updated to Firebase at ${currentTime}`);
}
} else {

await collabRef.set({
roomId: id,
...periodicData
});
console.log(`New Collab page recorded to Firebase at ${currentTime}`);
console.log(`New Collab page for roomid ${id} recorded to Firebase at ${currentTime}`);
}

} catch (error) {
Expand Down Expand Up @@ -117,6 +119,7 @@ const handleSocketIO = (io) => {
activeUserInRoom[socket.roomId] = activeUserInRoom[socket.roomId] - 1;

if(activeUserInRoom[socket.roomId] == 0) {
console.log(`All users in roomId ${socket.roomId} disconnected, deleting room data`);
delete activeUserInRoom[socket.roomId];

clearInterval(intervalMap[socket.roomId]);
Expand All @@ -133,7 +136,7 @@ const handleSocketIO = (io) => {
break;
}
}
console.log(`User with socket ID ${socket.id} disconnected`);
console.log(`User with socket ID ${socket.id} disconnected, leaving ${socket.roomId}`);
});


Expand Down

0 comments on commit 78510be

Please sign in to comment.