UNPKG

847 BPlain TextView Raw
1import { IStompSocket } from './types';
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
20 const origOnClose = this.onclose;
21
22 // Track delay in actual closure of the socket
23 this.onclose = closeEvent => {
24 const delay = new Date().getTime() - ts.getTime();
25 debug(
26 `Discarded socket closed after ${delay}ms, with code/reason: ${closeEvent.code}/${closeEvent.reason}`
27 );
28 };
29
30 this.close();
31
32 origOnClose.call(this, {
33 code: 4001,
34 reason: 'Heartbeat failure, discarding the socket',
35 wasClean: false,
36 });
37 };
38}