diff --git a/src/app/room/page.tsx b/src/app/room/page.tsx index 92d46ca..2fa1ba9 100644 --- a/src/app/room/page.tsx +++ b/src/app/room/page.tsx @@ -222,9 +222,12 @@ function RoomInner() { s.on("connect", () => { if (sessionIdRef.current) { - s.emit("session:join", { sessionId: sessionIdRef.current }); + s.emit("session:join", { sessionId: sessionIdRef.current }, (error?: string) => { + if (!error) setSocket(s); + }); + } else { + setSocket(s); } - setSocket(s); }); s.on("session:ended", () => { diff --git a/src/socket/index.ts b/src/socket/index.ts index 9a40487..8a5f69f 100644 --- a/src/socket/index.ts +++ b/src/socket/index.ts @@ -137,7 +137,7 @@ export async function initSocketIO( console.log(`[Socket.IO] Client connected: ${socket.id} (user: ${socket.data.userId})`); // Session room management - socket.on("session:join", async (payload) => { + socket.on("session:join", async (payload, ack) => { if (payload?.sessionId && typeof payload.sessionId === "string") { const userId = socket.data.userId; @@ -149,6 +149,7 @@ export async function initSocketIO( if (!sessionRecord) { socket.emit("question:error", { message: "Session not found." }); + if (typeof ack === "function") ack("Session not found."); return; } @@ -173,6 +174,7 @@ export async function initSocketIO( reason: "not enrolled", }); socket.emit("question:error", { message: "You are not enrolled in this session." }); + if (typeof ack === "function") ack("Not enrolled."); return; } @@ -192,6 +194,7 @@ export async function initSocketIO( } await broadcastViewerCount(payload.sessionId); + if (typeof ack === "function") ack(); } }); diff --git a/src/socket/types.ts b/src/socket/types.ts index 7f9249b..9a5df5f 100644 --- a/src/socket/types.ts +++ b/src/socket/types.ts @@ -165,7 +165,7 @@ export interface ViewerSyncPayload { export interface ClientToServerEvents { "question:create": (payload: QuestionCreatePayload) => void; "answer:create": (payload: AnswerCreatePayload) => void; - "session:join": (payload: SessionJoinPayload) => void; + "session:join": (payload: SessionJoinPayload, ack?: (error?: string) => void) => void; "session:leave": (payload: SessionLeavePayload) => void; "question:upvote": (payload: QuestionUpvotePayload) => void; "answer:upvote": (payload: AnswerUpvotePayload) => void;