UNPKG

976 BPlain TextView Raw
1import { IStompSocket } from './types.js';
2
3/**
4 * @internal
5 */
6export function augmentWebsocket(
7 webSocket: IStompSocket,
8 debug: (msg: string) => void
9) {
10 webSocket.terminate = function () {
11 const noOp = () => {};
12
13 // set all callbacks to no op
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); // A simulated id
20
21 const origOnClose = this.onclose;
22
23 // Track delay in actual closure of the socket
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}