-
Notifications
You must be signed in to change notification settings - Fork 64
/
mux.ts
63 lines (52 loc) · 1.63 KB
/
mux.ts
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import debug from "debug";
const log = debug("WebIO:Mux");
import WebIO from "@webio/webio";
/**
* Get the standard url of the Mux/WebIO WebSocket.
*/
export const getMuxWSUrl = (): string => {
const {protocol, host, pathname} = window.location;
const wsProtocol = protocol == "https:" ? "wss:" : "ws:";
const basePath = pathname[pathname.length - 1] == "/" ? pathname : pathname + "/";
const wsPath = basePath + "webio-socket";
return `${wsProtocol}//${host}${wsPath}`;
};
/**
* Create a WebIO instance connected to a Mux WebSocket.
* @param webIO - the WebIO instance to connect.
* @param wsUrl - the url of the WebSocket to connect to.
*/
export const connectWebIOToWebSocket = (
webIO: WebIO,
wsUrl: string = getMuxWSUrl(),
) => {
return new Promise<void>((resolve, reject) => {
const webSocket = new WebSocket(wsUrl);
webSocket.onopen = () => {
webIO.setSendCallback((msg: any) => {
webSocket.send(JSON.stringify(msg));
});
resolve();
};
webSocket.onclose = (closeEvent) => {
// Rejecting a resolved or already-rejected promise is a no-op.
reject(closeEvent);
};
webSocket.onerror = (error) => {
// Rejecting a resolved or already-rejected promise is a no-op.
reject(error);
};
webSocket.onmessage = ({data}) => {
const message = JSON.parse(data);
webIO.dispatch(message);
}
})
};
const muxEntrypoint = async () => {
const webIO = new WebIO();
// We do window as any to allow defining new members.
(window as any).WebIO = webIO;
await connectWebIOToWebSocket(webIO);
log("Connected WebIO to WebSocket.")
};
muxEntrypoint();