Skip to content

Commit

Permalink
Refactor socket event handlers and add join room functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
loicBRAVO committed Feb 13, 2024
1 parent f499de5 commit 1607a87
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
26 changes: 23 additions & 3 deletions backend/src/routes/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`);
Expand Down Expand Up @@ -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);

Expand Down
33 changes: 29 additions & 4 deletions frontend/src/components/Chat/ChatFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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]);
Expand All @@ -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");
Expand Down

0 comments on commit 1607a87

Please sign in to comment.