almost ready
This commit is contained in:
parent
d66b3f9f5f
commit
4c65c6f4ed
|
|
@ -5,7 +5,6 @@ import { addInput, setStatus, addOutput } from "./scrollback.js"
|
||||||
import { send } from "./websocket.js"
|
import { send } from "./websocket.js"
|
||||||
import { randomID } from "../shared/utils.js"
|
import { randomID } from "../shared/utils.js"
|
||||||
import type { Message, CommandResult } from "../shared/types.js"
|
import type { Message, CommandResult } from "../shared/types.js"
|
||||||
import type { CompoundAssignmentOperator } from "typescript"
|
|
||||||
|
|
||||||
export function runCommand(input: string) {
|
export function runCommand(input: string) {
|
||||||
const id = randomID()
|
const id = randomID()
|
||||||
|
|
@ -15,9 +14,7 @@ export function runCommand(input: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// message received from server
|
// message received from server
|
||||||
export function handleMessage(e: MessageEvent) {
|
export function handleMessage(data: Message) {
|
||||||
const data = JSON.parse(e.data)
|
|
||||||
console.log("-> receive", data)
|
|
||||||
if (data.type === "output") {
|
if (data.type === "output") {
|
||||||
handleOutput(data)
|
handleOutput(data)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ export function startConnection() {
|
||||||
ws = new WebSocket(url)
|
ws = new WebSocket(url)
|
||||||
|
|
||||||
ws.onopen = () => console.log('WS connected')
|
ws.onopen = () => console.log('WS connected')
|
||||||
ws.onmessage = handleMessage
|
ws.onmessage = receive
|
||||||
ws.onclose = () => setTimeout(startConnection, 1000) // simple retry
|
ws.onclose = () => setTimeout(startConnection, 1000) // simple retry
|
||||||
ws.onerror = () => ws?.close()
|
ws.onerror = () => ws?.close()
|
||||||
}
|
}
|
||||||
|
|
@ -26,13 +26,14 @@ export function send(msg: Message) {
|
||||||
console.log("-> send", msg)
|
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
|
// close it... plz don't do this, though
|
||||||
export function close() {
|
export function close() {
|
||||||
ws?.close(1000, 'bye')
|
ws?.close(1000, 'bye')
|
||||||
}
|
}
|
||||||
|
|
||||||
// register a local listener
|
|
||||||
export function onMessage(callback: (e: MessageEvent) => void) {
|
|
||||||
if (!ws) return
|
|
||||||
ws.onmessage = callback
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -52,12 +52,12 @@ app.on("GET", ["/js/:path{.+}", "/shared/:path{.+}"], async c => {
|
||||||
// websocket
|
// websocket
|
||||||
//
|
//
|
||||||
|
|
||||||
const connections: any[] = []
|
const wsConnections: any[] = []
|
||||||
|
|
||||||
app.get("/ws", upgradeWebSocket((c) => {
|
app.get("/ws", upgradeWebSocket((c) => {
|
||||||
return {
|
return {
|
||||||
onOpen(_e, ws) {
|
onOpen(_e, ws) {
|
||||||
connections.push(ws)
|
wsConnections.push(ws)
|
||||||
},
|
},
|
||||||
onMessage(event, ws) {
|
onMessage(event, ws) {
|
||||||
let data: Message | undefined
|
let data: Message | undefined
|
||||||
|
|
@ -125,7 +125,7 @@ if (process.env.BUN_HOT) {
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
globalThis.__hot_reload_cleanup = () => {
|
globalThis.__hot_reload_cleanup = () => {
|
||||||
connections.forEach(conn => conn?.close())
|
wsConnections.forEach(conn => conn?.close())
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const sig of ["SIGINT", "SIGTERM"] as const) {
|
for (const sig of ["SIGINT", "SIGTERM"] as const) {
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
// runs commands and such.
|
// runs commands and such.
|
||||||
|
|
||||||
import type { CommandResult } from "./shared/types"
|
import type { CommandResult } from "./shared/types"
|
||||||
|
import { NOSE_BIN } from "./config"
|
||||||
|
|
||||||
export function runCommand(input: string): CommandResult {
|
export function runCommand(input: string): CommandResult {
|
||||||
return { status: "ok", output: "command: " + input }
|
return { status: "ok", output: "command: " + input }
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user