Skip to content

Commit

Permalink
refactor: add list of possible disconnection reasons
Browse files Browse the repository at this point in the history
Note: some disconnection reasons could be merged in the next major
release, i.e. the Deno impl does not have "forced server close" and
"server shutting down"

Related: #4387
  • Loading branch information
darrachequesne committed Sep 13, 2022
1 parent 8be95b3 commit 10fa4a2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
9 changes: 8 additions & 1 deletion lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ interface WriteOptions {
wsPreEncoded?: string;
}

type CloseReason =
| "transport error"
| "transport close"
| "forced close"
| "ping timeout"
| "parse error";

export class Client<
ListenEvents extends EventsMap,
EmitEvents extends EventsMap,
Expand Down Expand Up @@ -306,7 +313,7 @@ export class Client<
* @param reason
* @private
*/
private onclose(reason: string): void {
private onclose(reason: CloseReason | "forced server close"): void {
debug("client close with reason %s", reason);

// ignore a potential subsequent `close` event
Expand Down
20 changes: 17 additions & 3 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,23 @@ const debug = debugModule("socket.io:socket");

type ClientReservedEvents = "connect_error";

// TODO for next major release: cleanup disconnect reasons
export type DisconnectReason =
// Engine.IO close reasons
| "transport error"
| "transport close"
| "forced close"
| "ping timeout"
| "parse error"
// Socket.IO disconnect reasons
| "server shutting down"
| "forced server close"
| "client namespace disconnect"
| "server namespace disconnect";

export interface SocketReservedEventsMap {
disconnect: (reason: string) => void;
disconnecting: (reason: string) => void;
disconnect: (reason: DisconnectReason) => void;
disconnecting: (reason: DisconnectReason) => void;
error: (err: Error) => void;
}

Expand Down Expand Up @@ -509,7 +523,7 @@ export class Socket<
*
* @private
*/
_onclose(reason: string): this | undefined {
_onclose(reason: DisconnectReason): this | undefined {
if (!this.connected) return this;
debug("closing socket - reason %s", reason);
this.emitReserved("disconnecting", reason);
Expand Down
5 changes: 3 additions & 2 deletions test/socket.io.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { DefaultEventsMap } from "../lib/typed-events";
import { createServer } from "http";
import { expectError, expectType } from "tsd";
import { Adapter } from "socket.io-adapter";
import type { DisconnectReason } from "../lib/socket";

// This file is run by tsd, not mocha.

Expand All @@ -17,10 +18,10 @@ describe("server", () => {
sio.on("connection", (s) => {
expectType<Socket<DefaultEventsMap, DefaultEventsMap>>(s);
s.on("disconnect", (reason) => {
expectType<string>(reason);
expectType<DisconnectReason>(reason);
});
s.on("disconnecting", (reason) => {
expectType<string>(reason);
expectType<DisconnectReason>(reason);
});
});
sio.on("connect", (s) => {
Expand Down

0 comments on commit 10fa4a2

Please sign in to comment.