Skip to content

feat: add details to the disconnecting and disconnect events #4622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,17 +311,18 @@ export class Client<
* Called upon transport close.
*
* @param reason
* @param description
* @private
*/
private onclose(reason: CloseReason | "forced server close"): void {
private onclose(reason: CloseReason | "forced server close", description?: any): void {
debug("client close with reason %s", reason);

// ignore a potential subsequent `close` event
this.destroy();

// `nsps` and `sockets` are cleaned up seamlessly
for (const socket of this.sockets.values()) {
socket._onclose(reason);
socket._onclose(reason, description);
}
this.sockets.clear();

Expand Down
11 changes: 6 additions & 5 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ const RECOVERABLE_DISCONNECT_REASONS: ReadonlySet<DisconnectReason> = new Set([
]);

export interface SocketReservedEventsMap {
disconnect: (reason: DisconnectReason) => void;
disconnecting: (reason: DisconnectReason) => void;
disconnect: (reason: DisconnectReason, description?: any) => void;
disconnecting: (reason: DisconnectReason, description?: any) => void;
error: (err: Error) => void;
}

Expand Down Expand Up @@ -732,14 +732,15 @@ export class Socket<
* Called upon closing. Called by `Client`.
*
* @param {String} reason
* @param description
* @throw {Error} optional error object
*
* @private
*/
_onclose(reason: DisconnectReason): this | undefined {
_onclose(reason: DisconnectReason, description?: any): this | undefined {
if (!this.connected) return this;
debug("closing socket - reason %s", reason);
this.emitReserved("disconnecting", reason);
this.emitReserved("disconnecting", reason, description);

if (RECOVERABLE_DISCONNECT_REASONS.has(reason)) {
debug("connection state recovery is enabled for sid %s", this.id);
Expand All @@ -755,7 +756,7 @@ export class Socket<
this.nsp._remove(this);
this.client._remove(this);
this.connected = false;
this.emitReserved("disconnect", reason);
this.emitReserved("disconnect", reason, description);
return;
}

Expand Down