diff --git a/backend/server/src/index.ts b/backend/server/src/index.ts index a13d39c..d03ad98 100644 --- a/backend/server/src/index.ts +++ b/backend/server/src/index.ts @@ -151,7 +151,7 @@ io.on("connection", async (socket) => { }) await saveFile(fileId, body) } catch (e) { - socket.emit("rateLimit", "Rate limited: file saving. Please slow down.") + io.emit("rateLimit", "Rate limited: file saving. Please slow down.") } }) @@ -178,7 +178,7 @@ io.on("connection", async (socket) => { await createFile(id) } catch (e) { - socket.emit("rateLimit", "Rate limited: file creation. Please slow down.") + io.emit("rateLimit", "Rate limited: file creation. Please slow down.") } }) @@ -203,7 +203,7 @@ io.on("connection", async (socket) => { ) await renameFile(fileId, newFileId, file.data) } catch (e) { - socket.emit("rateLimit", "Rate limited: file renaming. Please slow down.") + io.emit("rateLimit", "Rate limited: file renaming. Please slow down.") return } }) @@ -226,7 +226,7 @@ io.on("connection", async (socket) => { const newFiles = await getSandboxFiles(data.sandboxId) callback(newFiles.files) } catch (e) { - socket.emit("rateLimit", "Rate limited: file deletion. Please slow down.") + io.emit("rateLimit", "Rate limited: file deletion. Please slow down.") } }) @@ -243,7 +243,8 @@ io.on("connection", async (socket) => { }) const onData = pty.onData((data) => { - socket.emit("terminalResponse", { + console.log("terminalResponse", id, data) + io.emit("terminalResponse", { id, data, }) @@ -259,11 +260,13 @@ io.on("connection", async (socket) => { onExit, } - callback(true) + callback() }) socket.on("terminalData", (id: string, data: string) => { + console.log("terminalData", id, data) if (!terminals[id]) { + console.log("terminal not found", id) return } @@ -279,11 +282,14 @@ io.on("connection", async (socket) => { return } + console.log("closing terminal", id) terminals[id].onData.dispose() terminals[id].onExit.dispose() delete terminals[id] - callback(true) + console.log("terminals:", Object.keys(terminals)) + + callback() }) socket.on( @@ -324,11 +330,12 @@ io.on("connection", async (socket) => { socket.on("disconnect", async () => { if (data.isOwner) { + console.log("deleting all terminals") Object.entries(terminals).forEach((t) => { - const { terminal, onData, onExit } = t[1] - onData.dispose() - onExit.dispose() - delete terminals[t[0]] + const { terminal, onData, onExit } = t[1] + onData.dispose() + onExit.dispose() + delete terminals[t[0]] }) socket.broadcast.emit("disableAccess", "The sandbox owner has disconnected.") diff --git a/frontend/components/editor/index.tsx b/frontend/components/editor/index.tsx index 85b34e9..a55f291 100644 --- a/frontend/components/editor/index.tsx +++ b/frontend/components/editor/index.tsx @@ -359,14 +359,11 @@ export default function CodeEditor({ useEffect(() => { const onConnect = () => { console.log("connected"); - - createTerminal(); }; const onDisconnect = () => { console.log("disconnected"); - - closeAllTerminals(); + setTerminals([]); }; const onLoadedEvent = (files: (TFolder | TFile)[]) => { @@ -378,11 +375,14 @@ export default function CodeEditor({ }; const onTerminalResponse = (response: { id: string; data: string }) => { - const res = response.data; - console.log("terminal response:", res); + console.log("terminal response:", response); + // console.log("activeId:", activeTerminalId); - const term = terminals.find((t) => t.id === response.id); - if (term && term.terminal) term.terminal.write(res); + // const term = terminals.find((t) => t.id === response.id); + // if (term && term.terminal) { + // console.log("writing to terminal, id:", response.id); + // term.terminal.write(response.data); + // } }; const onDisableAccess = (message: string) => { @@ -414,15 +414,14 @@ export default function CodeEditor({ const createTerminal = () => { setCreatingTerminal(true); const id = createId(); - setActiveTerminalId(id); + console.log("creating terminal, id:", id); setTimeout(() => { - socket.emit("createTerminal", id, (res: boolean) => { - if (res) { - setTerminals((prev) => [...prev, { id, terminal: null }]); - } + socket.emit("createTerminal", id, () => { + setTerminals((prev) => [...prev, { id, terminal: null }]); + setActiveTerminalId(id); + setCreatingTerminal(false); }); }, 1000); - setCreatingTerminal(false); }; const selectFile = (tab: TTab) => { @@ -476,38 +475,29 @@ export default function CodeEditor({ const index = terminals.findIndex((t) => t.id === term.id); if (index === -1) return; - socket.emit("closeTerminal", term.id, (res: boolean) => { - if (res) { - const nextId = - activeTerminalId === term.id - ? numTerminals === 1 - ? null - : index < numTerminals - 1 - ? terminals[index + 1].id - : terminals[index - 1].id - : activeTerminalId; + socket.emit("closeTerminal", term.id, () => { + const nextId = + activeTerminalId === term.id + ? numTerminals === 1 + ? null + : index < numTerminals - 1 + ? terminals[index + 1].id + : terminals[index - 1].id + : activeTerminalId; - setTerminals((prev) => prev.filter((t) => t.id !== term.id)); + setTerminals((prev) => prev.filter((t) => t.id !== term.id)); - if (!nextId) { - setActiveTerminalId(""); - } else { - const nextTerminal = terminals.find((t) => t.id === nextId); - if (nextTerminal) { - setActiveTerminalId(nextTerminal.id); - } + if (!nextId) { + setActiveTerminalId(""); + } else { + const nextTerminal = terminals.find((t) => t.id === nextId); + if (nextTerminal) { + setActiveTerminalId(nextTerminal.id); } } }); }; - const closeAllTerminals = () => { - terminals.forEach((term) => { - socket.emit("closeTerminal", term.id, () => {}); // no need to wait for response here - setTerminals((prev) => prev.filter((t) => t.id !== term.id)); - }); - }; - const handleRename = ( id: string, newName: string, @@ -781,24 +771,34 @@ export default function CodeEditor({ )} -