32 lines
761 B
TypeScript
32 lines
761 B
TypeScript
import type { Message } from '../shared/types'
|
|
|
|
const wsConnections = new Map<string, any>()
|
|
|
|
export function send(ws: any, msg: Message) {
|
|
console.log("-> send", msg)
|
|
ws.send(JSON.stringify(msg))
|
|
}
|
|
|
|
export function sendTo(session: string, msg: Message) {
|
|
const ws = wsConnections.get(session)
|
|
if (ws) send(ws, msg)
|
|
else console.error('sendTo session not found:', session)
|
|
}
|
|
|
|
export function sendAll(msg: Message) {
|
|
wsConnections.forEach(ws => send(ws, msg))
|
|
}
|
|
|
|
export function addWebsocket(session: string, ws: any) {
|
|
wsConnections.set(session, ws)
|
|
}
|
|
|
|
export function removeWebsocket(session: string) {
|
|
wsConnections.delete(session)
|
|
}
|
|
|
|
export function closeWebsockets() {
|
|
wsConnections.forEach(conn => conn.close())
|
|
wsConnections.clear()
|
|
}
|