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 = (
|
export const handleJoinSession = (
|
||||||
socket: Socket,
|
socket: Socket,
|
||||||
sessionId: string,
|
sessionId: string,
|
||||||
callback: Function
|
callback: Function | undefined
|
||||||
) => {
|
) => {
|
||||||
const isSessionRunning = allGames.some(
|
const isSessionRunning = allGames.some(
|
||||||
(game) => game.sessionId === sessionId
|
(game) => game.sessionId === sessionId
|
||||||
);
|
);
|
||||||
if (isSessionRunning) {
|
if (isSessionRunning) {
|
||||||
callback("error:running" satisfies SessionResponse);
|
callback && callback("error:running" satisfies SessionResponse);
|
||||||
socket.emit(
|
socket.emit(
|
||||||
"message",
|
"message",
|
||||||
"A Game is already running in this session. Please join another session."
|
"A Game is already running in this session. Please join another session."
|
||||||
|
|
@ -24,7 +24,7 @@ export const handleJoinSession = (
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.join(sessionId);
|
socket.join(sessionId);
|
||||||
callback("success" satisfies SessionResponse);
|
callback && callback("success" satisfies SessionResponse);
|
||||||
console.log("User joined session:", sessionId);
|
console.log("User joined session:", sessionId);
|
||||||
|
|
||||||
const numberOfClients = io.sockets.adapter.rooms.get(sessionId)?.size ?? 0;
|
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) => {
|
io.on("connection", (socket: Socket) => {
|
||||||
console.log("A user connected:", socket.id);
|
console.log("A user connected:", socket.id);
|
||||||
|
|
||||||
socket.on("join-session", (sessionId: string, callback) =>
|
socket.on(
|
||||||
handleJoinSession(socket, sessionId, callback)
|
"join-session",
|
||||||
|
(sessionId: string, callback: Function | undefined) => {
|
||||||
|
console.log(typeof callback);
|
||||||
|
handleJoinSession(socket, sessionId, callback);
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
socket.on("leave-session", (sessionId: string) => {
|
socket.on("leave-session", (sessionId: string) => {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue