From add4ead7e701944eca7286b000f8ac41bdfc1a76 Mon Sep 17 00:00:00 2001 From: Deveshi Dwivedi Date: Sun, 23 Jun 2024 14:26:33 +0530 Subject: [PATCH] add socket event, minor tweaks --- modules/home/components/Home.tsx | 34 +++++++++---------- modules/room/components/NameInput.tsx | 2 +- server/index.ts | 48 ++++++++++++++++++--------- 3 files changed, 50 insertions(+), 34 deletions(-) diff --git a/modules/home/components/Home.tsx b/modules/home/components/Home.tsx index 97438fd..06a9cd0 100644 --- a/modules/home/components/Home.tsx +++ b/modules/home/components/Home.tsx @@ -54,24 +54,24 @@ const Home = () => { return (
-

+

SketchParty

Sketching and Fun for Everyone!

-
- - setUsername(e.target.value)} - > - -
- //recent changes -
+
+ + setUsername(e.target.value)} + /> +
+ + + @@ -90,14 +90,14 @@ const Home = () => {
-
+
-

Or

+

OR

-
+
Create a New Room
diff --git a/modules/room/components/NameInput.tsx b/modules/room/components/NameInput.tsx index 9871748..ae12674 100644 --- a/modules/room/components/NameInput.tsx +++ b/modules/room/components/NameInput.tsx @@ -46,7 +46,7 @@ const NameInput = () => {

SketchParty

Sketching and Fun for everyone

-
+
setName(e.target.value)}>
diff --git a/server/index.ts b/server/index.ts index f2ec2f1..e095d6d 100644 --- a/server/index.ts +++ b/server/index.ts @@ -25,14 +25,14 @@ nextApp.prepare().then(async () => { const room = rooms.get(roomId); if (!room?.users.has(socketId)) { - room?.users.set(socketId, [move]); + room?.usersMoves.set(socketId, [move]); } - room?.users.get(socketId)?.push(move); + room?.usersMoves.get(socketId)?.push(move); }; const undoMove = (roomId: string, socketId: string) => { const room = rooms.get(roomId); - room?.users.get(socketId)?.pop(); + room?.usersMoves.get(socketId)?.pop(); }; const leaveRoom = (roomId: string, socketId: string) => { @@ -40,7 +40,7 @@ nextApp.prepare().then(async () => { if(!room) return; - const userMoves= room?.users.get(socketId)!; + const userMoves= room?.usersMoves.get(socketId)!; room?.drawed.push(...userMoves); room?.users.delete(socketId); @@ -56,28 +56,40 @@ nextApp.prepare().then(async () => { } console.log("connection"); - socket.on("create_room", () => { + socket.on("create_room", (username) => { let roomId: string; do{ roomId = Math.random().toString(36).substring(2,6); } while (rooms.has(roomId)); socket.join(roomId); - rooms.set(roomId, {users: new Map(), drawed: []}); - rooms.get(roomId)?.users.set(socket.id, []); - + rooms.set(roomId, { + users: new Map([[socket.id, username]]), + drawed: [], + usersMoves: new Map([[socket.id, []]]), + }); + io.to(socket.id).emit("created", roomId); }); - socket.on("join_room", (roomId: string) => { - if(rooms.has(roomId)){ + socket.on("join_room", (roomId, username) => { + const room=rooms.get(roomId); + if(room){ socket.join(roomId); + room.users.set(socket.id, username); + room.usersMoves.set(socket.id, []); + io.to(socket.id).emit("joined", roomId); } else io.to(socket.id).emit("joined", "", true); }); + socket.on("check_room", (roomId)=> { + if(rooms.has(roomId)) socket.emit("room_exists", true); + else socket.emit("room_exists", false); + }) + socket.on("joined_room",() => { console.log("joined room"); @@ -85,13 +97,17 @@ nextApp.prepare().then(async () => { //add the socket to the room's map const room= rooms.get(roomId); - if(room) { - room.users.set(socket.id, []); - io.to(socket.id).emit("room", room, JSON.stringify([...room.users])); - - socket.broadcast.to(roomId).emit("new_user", socket.id); - }; + if(!room) return; + + io.to(socket.id).emit( + "room", + room, + JSON.stringify([...room.usersMoves]), + JSON.stringify([...room.users]) + ); + + socket.broadcast.to(roomId).emit("new_user", socket.id, room.users.get(socket.id) || "Anonymous"); });