UNPKG

1 kBJavaScriptView Raw
1/**
2 * @internal
3 */
4export function augmentWebsocket(webSocket, debug) {
5 webSocket.terminate = function () {
6 const noOp = () => { };
7 // set all callbacks to no op
8 this.onerror = noOp;
9 this.onmessage = noOp;
10 this.onopen = noOp;
11 const ts = new Date();
12 const id = Math.random().toString().substring(2, 8); // A simulated id
13 const origOnClose = this.onclose;
14 // Track delay in actual closure of the socket
15 this.onclose = closeEvent => {
16 const delay = new Date().getTime() - ts.getTime();
17 debug(`Discarded socket (#${id}) closed after ${delay}ms, with code/reason: ${closeEvent.code}/${closeEvent.reason}`);
18 };
19 this.close();
20 origOnClose?.call(webSocket, {
21 code: 4001,
22 reason: `Quick discarding socket (#${id}) without waiting for the shutdown sequence.`,
23 wasClean: false,
24 });
25 };
26}
27//# sourceMappingURL=augment-websocket.js.map
\No newline at end of file