nose-pluto/app/src/websocket.ts
Chris Wanstrath 1c3dd16517 reeeeeboooot
2025-09-22 14:55:32 -07:00

32 lines
661 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) {
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
}