1 | import { IStompSocket } from './types.js';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | export function augmentWebsocket(
|
7 | webSocket: IStompSocket,
|
8 | debug: (msg: string) => void
|
9 | ) {
|
10 | webSocket.terminate = function () {
|
11 | const noOp = () => {};
|
12 |
|
13 |
|
14 | this.onerror = noOp;
|
15 | this.onmessage = noOp;
|
16 | this.onopen = noOp;
|
17 |
|
18 | const ts = new Date();
|
19 | const id = Math.random().toString().substring(2, 8);
|
20 |
|
21 | const origOnClose = this.onclose;
|
22 |
|
23 |
|
24 | this.onclose = closeEvent => {
|
25 | const delay = new Date().getTime() - ts.getTime();
|
26 | debug(
|
27 | `Discarded socket (#${id}) closed after ${delay}ms, with code/reason: ${closeEvent.code}/${closeEvent.reason}`
|
28 | );
|
29 | };
|
30 |
|
31 | this.close();
|
32 |
|
33 | origOnClose?.call(webSocket, {
|
34 | code: 4001,
|
35 | reason: `Quick discarding socket (#${id}) without waiting for the shutdown sequence.`,
|
36 | wasClean: false,
|
37 | });
|
38 | };
|
39 | }
|