From e4398166713c9b61af4b1f85ba6a2ba1076393dc Mon Sep 17 00:00:00 2001 From: James Murdza Date: Thu, 24 Oct 2024 17:10:23 -0600 Subject: [PATCH] refactor: keep disconnect handler in main file --- backend/server/src/SocketHandlers.ts | 16 ---------------- backend/server/src/index.ts | 14 +++++++++++++- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/backend/server/src/SocketHandlers.ts b/backend/server/src/SocketHandlers.ts index 5de26c3..1285280 100644 --- a/backend/server/src/SocketHandlers.ts +++ b/backend/server/src/SocketHandlers.ts @@ -116,20 +116,4 @@ export function handleCloseTerminal(terminalManager: TerminalManager, id: string export function handleGenerateCode(aiWorker: AIWorker, userId: string, fileName: string, code: string, line: number, instructions: string) { return aiWorker.generateCode(userId, fileName, code, line, instructions) } - -// Handle a client disconnecting from the server -export async function handleDisconnect(data: any, connections: any, terminalManager: TerminalManager, fileManager: FileManager, socket: any) { - if (data.isOwner) { - connections[data.sandboxId]-- - } - - await terminalManager.closeAllTerminals() - await fileManager.closeWatchers() - - if (data.isOwner && connections[data.sandboxId] <= 0) { - socket.broadcast.emit( - "disableAccess", - "The sandbox owner has disconnected." - ) - } } \ No newline at end of file diff --git a/backend/server/src/index.ts b/backend/server/src/index.ts index 1e06bb7..794af71 100644 --- a/backend/server/src/index.ts +++ b/backend/server/src/index.ts @@ -398,7 +398,19 @@ io.on("connection", async (socket) => { socket.on("disconnect", async () => { try { - await handleDisconnect(data, connections, terminalManager, fileManager, socket) + if (data.isOwner) { + connections[data.sandboxId]-- + } + + await terminalManager.closeAllTerminals() + await fileManager.closeWatchers() + + if (data.isOwner && connections[data.sandboxId] <= 0) { + socket.broadcast.emit( + "disableAccess", + "The sandbox owner has disconnected." + ) + } } catch (e: any) { console.log("Error disconnecting:", e) socket.emit("error", `Error: disconnecting. ${e.message ?? e}`)