Skip to content

Commit

Permalink
nit: handle case where there is more than 2 people in the room
Browse files Browse the repository at this point in the history
  • Loading branch information
HollaG committed Nov 4, 2024
1 parent 5607b29 commit 83ff4b3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions communication-service/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ function cleanupRoom(roomId) {
*/
function onJoinRoom(io, socket, data) {
const { roomId, user } = data;
// if there are already two people in this room, reject the request
if (io.sockets.adapter.rooms.get(roomId)?.size >= 2) {

console.log(`INFO: Room { ${roomId} } is full, rejecting user { ${user.userId} }`);
socket.emit('room-full');
return
}
onSetUsername(io, socket, user);

socket.join(roomId); // User joins the specified room
Expand Down
14 changes: 14 additions & 0 deletions peer-prep/src/components/Communication/Text/TextChatWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,24 @@ export default function TextChatWidget({ roomId }: TextChatWidgetProps) {
setMessages(chatHistory);
}

function onRoomFull() {
notifications.show({
title: "Room is full",
message: "This room is full, please try again later",
color: "red",
});

socket.disconnect();
}

socket.on("connect", onConnected);
socket.on("disconnect", onDisconnected);
socket.on("message-sent", onMessageSent); // for ack
socket.on("chat-message", onReceivedChatMessage);
socket.on("user-joined", onUserJoinedChat); // unused
socket.on("room-people-update", onRoomPeopleUpdate);
socket.on("chat-history", onReceiveChatHistory);
socket.on("room-full", onRoomFull);

return () => {
socket.disconnect();
Expand All @@ -291,6 +302,9 @@ export default function TextChatWidget({ roomId }: TextChatWidgetProps) {
socket.off("message-sent", onMessageSent);
socket.off("chat-message", onReceivedChatMessage);
socket.off("user-joined", onUserJoinedChat);
socket.off("room-people-update", onRoomPeopleUpdate);
socket.off("chat-history", onReceiveChatHistory);
socket.off("room-full", onRoomFull);
};
}, [roomId]);

Expand Down

0 comments on commit 83ff4b3

Please sign in to comment.