diff --git a/client/src/components/ParticipantsList.tsx b/client/src/components/ParticipantsList.tsx index a221cb6..645337d 100644 --- a/client/src/components/ParticipantsList.tsx +++ b/client/src/components/ParticipantsList.tsx @@ -5,10 +5,11 @@ function ParticipantList() { return ( <> -

Participants

diff --git a/client/src/pages/Room.tsx b/client/src/pages/Room.tsx index 1219425..19832b7 100644 --- a/client/src/pages/Room.tsx +++ b/client/src/pages/Room.tsx @@ -25,7 +25,7 @@ function Room() { ); } - // is connected + // View when is connected to the room return (
diff --git a/server/src/handleClientMessage.ts b/server/src/handleClientMessage.ts index 21e3201..efce2bf 100644 --- a/server/src/handleClientMessage.ts +++ b/server/src/handleClientMessage.ts @@ -6,6 +6,7 @@ import type { ServerMessageEditedMessage, ServerRegistrationConfirmed, ServerUserJoinedMessage, + ServerUserListMessage, User, } from "../../shared"; import type { Room, WebSocketData } from "./types"; @@ -45,6 +46,19 @@ export default function handleClientMessage( socket.send(JSON.stringify(joinMessage)); } + // send updated participants list + const userArray = Array.from(room.userConnections.values()); + const userListMessage: ServerUserListMessage = { + type: "USER_LIST", + payload: { + users: userArray, + }, + }; + + for (const [socket] of room.userConnections) { + socket.send(JSON.stringify(userListMessage)); + } + break; case "CHAT_MESSAGE": diff --git a/server/src/index.ts b/server/src/index.ts index 0711e17..9fd06ba 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -1,4 +1,8 @@ -import type { ClientMessage, ServerUserLeftMessage } from "../../shared.ts"; +import type { + ClientMessage, + ServerUserLeftMessage, + ServerUserListMessage, +} from "../../shared.ts"; import handleClientMessage from "./handleClientMessage.ts"; import type { Room, WebSocketData } from "./types.ts"; @@ -78,6 +82,21 @@ const server = Bun.serve({ } } + room.userConnections.delete(ws); + + // send updated user list + const userArray = Array.from(room.userConnections.values()); + const userListMessage: ServerUserListMessage = { + type: "USER_LIST", + payload: { + users: userArray, + }, + }; + + for (const [socket] of room.userConnections) { + socket.send(JSON.stringify(userListMessage)); + } + if (room.userConnections.size === 0) { rooms.delete(roomId); console.log("Cleaned up room", roomId);