Skip to content

Commit b862924

Browse files
feat: add details to the disconnect event
The "disconnect" event will now include additional details to help debugging if anything has gone wrong. Example when a payload is over the maxHttpBufferSize value in HTTP long-polling mode: ```js socket.on("disconnect", (reason, details) => { console.log(reason); // "transport error" // in that case, details is an error object console.log(details.message); "xhr post error" console.log(details.description); // 413 (the HTTP status of the response) // details.context refers to the XMLHttpRequest object console.log(details.context.status); // 413 console.log(details.context.responseText); // "" }); ``` Related: socketio/engine.io-client@b9252e2
1 parent eaf782c commit b862924

File tree

5 files changed

+81
-105
lines changed

5 files changed

+81
-105
lines changed

Diff for: lib/manager.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
SocketOptions as EngineOptions,
44
installTimerFunctions,
55
} from "engine.io-client";
6-
import { Socket, SocketOptions } from "./socket.js";
6+
import { Socket, SocketOptions, DisconnectDescription } from "./socket.js";
77
import * as parser from "socket.io-parser";
88
import { Decoder, Encoder, Packet } from "socket.io-parser";
99
import { on } from "./on.js";
@@ -90,7 +90,7 @@ interface ManagerReservedEvents {
9090
error: (err: Error) => void;
9191
ping: () => void;
9292
packet: (packet: Packet) => void;
93-
close: (reason: string) => void;
93+
close: (reason: string, description?: DisconnectDescription) => void;
9494
reconnect_failed: () => void;
9595
reconnect_attempt: (attempt: number) => void;
9696
reconnect_error: (err: Error) => void;
@@ -538,13 +538,13 @@ export class Manager<
538538
*
539539
* @private
540540
*/
541-
private onclose(reason: string): void {
541+
private onclose(reason: string, description?: DisconnectDescription): void {
542542
debug("closed due to %s", reason);
543543

544544
this.cleanup();
545545
this.backoff.reset();
546546
this._readyState = "closed";
547-
this.emitReserved("close", reason);
547+
this.emitReserved("close", reason, description);
548548

549549
if (this._reconnection && !this.skipReconnect) {
550550
this.reconnect();

Diff for: lib/socket.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,20 @@ interface Flags {
3939
timeout?: number;
4040
}
4141

42+
export type DisconnectDescription =
43+
| Error
44+
| {
45+
description: string;
46+
context?: CloseEvent | XMLHttpRequest;
47+
};
48+
4249
interface SocketReservedEvents {
4350
connect: () => void;
4451
connect_error: (err: Error) => void;
45-
disconnect: (reason: Socket.DisconnectReason) => void;
52+
disconnect: (
53+
reason: Socket.DisconnectReason,
54+
description?: DisconnectDescription
55+
) => void;
4656
}
4757

4858
export class Socket<
@@ -266,14 +276,18 @@ export class Socket<
266276
* Called upon engine `close`.
267277
*
268278
* @param reason
279+
* @param description
269280
* @private
270281
*/
271-
private onclose(reason: Socket.DisconnectReason): void {
282+
private onclose(
283+
reason: Socket.DisconnectReason,
284+
description?: DisconnectDescription
285+
): void {
272286
debug("close (%s)", reason);
273287
this.connected = false;
274288
this.disconnected = true;
275289
delete this.id;
276-
this.emitReserved("disconnect", reason);
290+
this.emitReserved("disconnect", reason, description);
277291
}
278292

279293
/**

Diff for: lib/url.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import parseuri from "parseuri";
1+
import { parse } from "engine.io-client";
22
import debugModule from "debug"; // debug()
33

44
const debug = debugModule("socket.io-client:url"); // debug()
@@ -68,7 +68,7 @@ export function url(
6868

6969
// parse
7070
debug("parse %s", uri);
71-
obj = parseuri(uri) as ParsedUrl;
71+
obj = parse(uri) as ParsedUrl;
7272
}
7373

7474
// make sure we treat `localhost:80` and `localhost` equally

Diff for: package-lock.json

+55-92
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@
3232
},
3333
"types": "./build/esm/index.d.ts",
3434
"dependencies": {
35-
"@socket.io/component-emitter": "~3.0.0",
35+
"@socket.io/component-emitter": "~3.1.0",
3636
"backo2": "~1.0.2",
3737
"debug": "~4.3.2",
38-
"engine.io-client": "~6.1.1",
39-
"parseuri": "0.0.6",
40-
"socket.io-parser": "~4.1.1"
38+
"engine.io-client": "~6.2.1",
39+
"socket.io-parser": "~4.2.0"
4140
},
4241
"devDependencies": {
4342
"@babel/core": "^7.15.0",

0 commit comments

Comments
 (0)