-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathSockJSClient.js
49 lines (45 loc) · 941 Bytes
/
SockJSClient.js
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
import SockJS from "../modules/sockjs-client/index.js";
import { log } from "../utils/log.js";
export default class SockJSClient {
/**
* @param {string} url
*/
constructor(url) {
// SockJS requires `http` and `https` protocols
this.sock = new SockJS(
url.replace(/^ws:/i, "http:").replace(/^wss:/i, "https:"),
);
this.sock.onerror =
/**
* @param {Error} error
*/
(error) => {
log.error(error);
};
}
/**
* @param {(...args: any[]) => void} f
*/
onOpen(f) {
this.sock.onopen = f;
}
/**
* @param {(...args: any[]) => void} f
*/
onClose(f) {
this.sock.onclose = f;
}
// call f with the message string as the first argument
/**
* @param {(...args: any[]) => void} f
*/
onMessage(f) {
this.sock.onmessage =
/**
* @param {Error & { data: string }} e
*/
(e) => {
f(e.data);
};
}
}