nose-pluto/src/websocket.ts
Chris Wanstrath c381b90f39 logging
2025-10-02 13:06:30 -07:00

33 lines
691 B
TypeScript

////
// Manage open websockets, send messages... fun stuff like that.
import type { Message } from "./shared/types"
const wsConnections: any[] = []
export function send(ws: any, msg: Message) {
console.log("-> send", msg)
ws.send(JSON.stringify(msg))
}
export function sendAll(msg: Message) {
wsConnections.forEach(ws => send(ws, msg))
}
export function addWebsocket(ws: any) {
wsConnections.push(ws)
}
export function removeWebsocket(ws: any) {
wsConnections.splice(wsConnections.indexOf(ws), 1)
}
export function closeWebsockets() {
wsConnections.forEach(conn => conn.close())
wsConnections.length = 0
}
export function websockets(): any[] {
return wsConnections
}