blob: 56f11fdf76204ca03861d28ac1e516e6d74c5664 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
let ws_handler_registry = {}
export const init_websocket = path => {
let ws
if (typeof WebSocket === "function") {
// we're in the browser
let url = new URL(document.URL)
let protocol = url.protocol === "http:" ? "ws" : "wss"
let ws_url = protocol + "://" + url.host + url.pathname + path
ws = new WebSocket(ws_url)
} else {
// we're NOT in the browser, prolly running tests
ws = {}
}
ws_handler_registry[ws.url] = { ws: ws }
ws.onopen = evt => {
ws_handler_registry[ws.url]?.on_open?.()
}
ws.onclose = evt => {
ws_handler_registry[ws.url]?.on_close?.(evt.code)
delete ws_handler_registry[ws.url]
}
ws.onmessage = event => ws_handler_registry[ws.url]?.on_message?.(event.data)
ws.onerror = error => console.log("ws", ws.url, "error", error, "no handler, since I have no clue what errors we might be talking about")
return ws
}
export const register_websocket_handler = (ws, on_open, on_message, on_close) => {
const reg_entry = ws_handler_registry[ws.url]
reg_entry.on_open = on_open
reg_entry.on_message = on_message
reg_entry.on_close = on_close
console.log("ws reg", ws_handler_registry)
}
export const send_over_websocket = (ws, msg) => {
ws.send(msg)
}
|