32 lines
661 B
TypeScript
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
|
|
}
|