refactor: keep disconnect handler in main file

This commit is contained in:
James Murdza 2024-10-24 17:10:23 -06:00
parent ef018385ef
commit e439816671
2 changed files with 13 additions and 17 deletions

View File

@ -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."
)
}
}

View File

@ -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}`)