-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSockJSClient.ts
More file actions
41 lines (36 loc) · 1.07 KB
/
SockJSClient.ts
File metadata and controls
41 lines (36 loc) · 1.07 KB
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
/**
* The following code is modified based on
* https://github.com/webpack/webpack-dev-server
*
* MIT Licensed
* Author Tobias Koppers @sokra
* Copyright (c) JS Foundation and other contributors
* https://github.com/webpack/webpack-dev-server/blob/main/LICENSE
*/
import SockJS from '../modules/sockjs-client/index.js';
import { CommunicationClient } from '../type.js';
import { log } from '../utils/log.js';
export default class SockJSClient implements CommunicationClient {
sock: WebSocket;
constructor(url: string) {
// 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);
};
}
onOpen(fn: (...args: unknown[]) => void) {
this.sock.onopen = fn;
}
onClose(fn: (...args: unknown[]) => void) {
this.sock.onclose = fn;
}
// call f with the message string as the first argument
onMessage(fn: (...args: unknown[]) => void) {
this.sock.onmessage = (err) => {
fn(err.data);
};
}
}