From 1607a87c4048ede5554792498a114ff14c6c644a Mon Sep 17 00:00:00 2001 From: Akkiristo Date: Tue, 13 Feb 2024 22:00:55 +0100 Subject: [PATCH] Refactor socket event handlers and add join room functionality --- backend/src/routes/socket.ts | 26 ++++++++++++++-- frontend/src/components/Chat/ChatFooter.tsx | 33 ++++++++++++++++++--- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/backend/src/routes/socket.ts b/backend/src/routes/socket.ts index 1900a62..fe3a1b9 100644 --- a/backend/src/routes/socket.ts +++ b/backend/src/routes/socket.ts @@ -77,10 +77,7 @@ io.on("connection", (socket) => { socket.on("delete_room", async (roomId) => { try { - // Delete the room from the database await Room.findByIdAndDelete(roomId); - - // Notify all clients that the room has been deleted io.emit("room_deleted", roomId); console.log(`Room ${roomId} deleted.`); @@ -112,6 +109,29 @@ io.on("connection", (socket) => { log(`User with ID: ${socket.id} joined room: ${roomId}`); }); + socket.on("join", async (roomId, username) => { + // Vérifie si la salle existe + let room = await Room.findById(roomId); + if (!room) { + // Si la salle n'existe pas, vous pouvez choisir de la créer ou d'envoyer une erreur + socket.emit("join_error", `Room ${roomId} does not exist.`); + return; + } + + // Rejoint la salle + socket.join(roomId); + roomUsers[roomId] = [...(roomUsers[roomId] ?? []), socket.id]; + + // Informe les autres utilisateurs de la salle + socket.to(roomId).emit("receive_message", { + text: `${username} has joined the room.`, + systemMessage: true, + }); + + // Confirme la jonction à l'utilisateur + socket.emit("room_joined", {roomName: room.name, roomId: roomId}); + }); + socket.on("send_message", async (data) => { io.emit("receive_message", data); diff --git a/frontend/src/components/Chat/ChatFooter.tsx b/frontend/src/components/Chat/ChatFooter.tsx index 4a18262..aedca94 100644 --- a/frontend/src/components/Chat/ChatFooter.tsx +++ b/frontend/src/components/Chat/ChatFooter.tsx @@ -63,10 +63,30 @@ function ChatFooter({ roomId }: { roomId: string }) { } }, [socket, roomId]); + useEffect(() => { + if (socket) { + socket.on("room_joined", (id, username) => { + toast.success(`Joined room: ${id.roomName}`); + setMyRooms((prevRooms) => [...prevRooms, { id: id.roomId, title: id.roomName }]); + }); + + socket.on("join_error", (errorMessage) => { + toast.error(errorMessage); + }); + + return () => { + socket.off("room_joined"); + socket.off("join_error"); + }; + } + }, [socket]); + + function handleCommand(commandString: string, socket: any) { const parts = commandString.substr(1).split(" "); const command = parts[0].toLowerCase(); const args = parts.slice(1); + switch (command) { case "nick": @@ -90,7 +110,7 @@ function ChatFooter({ roomId }: { roomId: string }) { let newRoom = { title: roomName, - id: newRoomId, // Use the constant + id: newRoomId, }; console.log("New room: ", newRoomId); setMyRooms([...myRooms, newRoom]); @@ -102,9 +122,14 @@ function ChatFooter({ roomId }: { roomId: string }) { socket?.emit("delete_room", roomId); toast.info("Deleting room..."); break; - case "join": - const joinParam = args.join(" "); - socket?.emit("join", joinParam); + case "join": + if (args.length === 0) { + toast.error("Please specify a room ID to join."); + } else { + const roomIdToJoin = args[0]; + const username = localStorage.getItem("name"); + socket.emit("join", roomIdToJoin, username); + } break; case "quit": const username = localStorage.getItem("name");