diff --git a/src/js/shell.ts b/src/js/shell.ts index 0fcc951..3ea62e0 100644 --- a/src/js/shell.ts +++ b/src/js/shell.ts @@ -5,7 +5,6 @@ import { addInput, setStatus, addOutput } from "./scrollback.js" import { send } from "./websocket.js" import { randomID } from "../shared/utils.js" import type { Message, CommandResult } from "../shared/types.js" -import type { CompoundAssignmentOperator } from "typescript" export function runCommand(input: string) { const id = randomID() @@ -15,9 +14,7 @@ export function runCommand(input: string) { } // message received from server -export function handleMessage(e: MessageEvent) { - const data = JSON.parse(e.data) - console.log("-> receive", data) +export function handleMessage(data: Message) { if (data.type === "output") { handleOutput(data) } else { diff --git a/src/js/websocket.ts b/src/js/websocket.ts index 5a1d520..2986bcb 100644 --- a/src/js/websocket.ts +++ b/src/js/websocket.ts @@ -14,7 +14,7 @@ export function startConnection() { ws = new WebSocket(url) ws.onopen = () => console.log('WS connected') - ws.onmessage = handleMessage + ws.onmessage = receive ws.onclose = () => setTimeout(startConnection, 1000) // simple retry ws.onerror = () => ws?.close() } @@ -26,13 +26,14 @@ export function send(msg: Message) { console.log("-> send", msg) } +function receive(e: MessageEvent) { + const data = JSON.parse(e.data) as Message + console.log("<- receive", data) + handleMessage(data) +} + // close it... plz don't do this, though export function close() { ws?.close(1000, 'bye') } -// register a local listener -export function onMessage(callback: (e: MessageEvent) => void) { - if (!ws) return - ws.onmessage = callback -} diff --git a/src/server.tsx b/src/server.tsx index 9fada4c..0fe9753 100644 --- a/src/server.tsx +++ b/src/server.tsx @@ -52,12 +52,12 @@ app.on("GET", ["/js/:path{.+}", "/shared/:path{.+}"], async c => { // websocket // -const connections: any[] = [] +const wsConnections: any[] = [] app.get("/ws", upgradeWebSocket((c) => { return { onOpen(_e, ws) { - connections.push(ws) + wsConnections.push(ws) }, onMessage(event, ws) { let data: Message | undefined @@ -125,7 +125,7 @@ if (process.env.BUN_HOT) { // @ts-ignore globalThis.__hot_reload_cleanup = () => { - connections.forEach(conn => conn?.close()) + wsConnections.forEach(conn => conn?.close()) } for (const sig of ["SIGINT", "SIGTERM"] as const) { diff --git a/src/shell.ts b/src/shell.ts index 5d2bc50..118e863 100644 --- a/src/shell.ts +++ b/src/shell.ts @@ -2,6 +2,7 @@ // runs commands and such. import type { CommandResult } from "./shared/types" +import { NOSE_BIN } from "./config" export function runCommand(input: string): CommandResult { return { status: "ok", output: "command: " + input }