-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathSockJSClient.js
More file actions
46 lines (41 loc) · 986 Bytes
/
SockJSClient.js
File metadata and controls
46 lines (41 loc) · 986 Bytes
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
import SockJS from "../modules/sockjs-client/index.js";
import { log } from "../utils/log.js";
/** @typedef {import("../index").EXPECTED_ANY} EXPECTED_ANY */
/**
* @implements {CommunicationClient}
*/
export default class SockJSClient {
/**
* @param {string} url 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 = (error) => {
log.error(error);
};
}
/**
* @param {(...args: EXPECTED_ANY[]) => void} fn function
*/
onOpen(fn) {
this.sock.onopen = fn;
}
/**
* @param {(...args: EXPECTED_ANY[]) => void} fn function
*/
onClose(fn) {
this.sock.onclose = fn;
}
// call f with the message string as the first argument
/**
* @param {(...args: EXPECTED_ANY[]) => void} fn function
*/
onMessage(fn) {
this.sock.onmessage = (err) => {
fn(err.data);
};
}
}