fixes callback crashes
This commit is contained in:
parent
d0f6a3fdf2
commit
33e78e8987
2 changed files with 9 additions and 5 deletions
|
|
@ -9,13 +9,13 @@ type SessionResponse = "success" | "error:full" | "error:running";
|
|||
export const handleJoinSession = (
|
||||
socket: Socket,
|
||||
sessionId: string,
|
||||
callback: Function
|
||||
callback: Function | undefined
|
||||
) => {
|
||||
const isSessionRunning = allGames.some(
|
||||
(game) => game.sessionId === sessionId
|
||||
);
|
||||
if (isSessionRunning) {
|
||||
callback("error:running" satisfies SessionResponse);
|
||||
callback && callback("error:running" satisfies SessionResponse);
|
||||
socket.emit(
|
||||
"message",
|
||||
"A Game is already running in this session. Please join another session."
|
||||
|
|
@ -24,7 +24,7 @@ export const handleJoinSession = (
|
|||
}
|
||||
|
||||
socket.join(sessionId);
|
||||
callback("success" satisfies SessionResponse);
|
||||
callback && callback("success" satisfies SessionResponse);
|
||||
console.log("User joined session:", sessionId);
|
||||
|
||||
const numberOfClients = io.sockets.adapter.rooms.get(sessionId)?.size ?? 0;
|
||||
|
|
|
|||
|
|
@ -34,8 +34,12 @@ export const io = new SocketIOServer(httpServer, {
|
|||
io.on("connection", (socket: Socket) => {
|
||||
console.log("A user connected:", socket.id);
|
||||
|
||||
socket.on("join-session", (sessionId: string, callback) =>
|
||||
handleJoinSession(socket, sessionId, callback)
|
||||
socket.on(
|
||||
"join-session",
|
||||
(sessionId: string, callback: Function | undefined) => {
|
||||
console.log(typeof callback);
|
||||
handleJoinSession(socket, sessionId, callback);
|
||||
}
|
||||
);
|
||||
|
||||
socket.on("leave-session", (sessionId: string) => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue