Skip to content

Commit 10fa4a2

Browse files
refactor: add list of possible disconnection reasons
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
1 parent 8be95b3 commit 10fa4a2

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

lib/client.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ interface WriteOptions {
1818
wsPreEncoded?: string;
1919
}
2020

21+
type CloseReason =
22+
| "transport error"
23+
| "transport close"
24+
| "forced close"
25+
| "ping timeout"
26+
| "parse error";
27+
2128
export class Client<
2229
ListenEvents extends EventsMap,
2330
EmitEvents extends EventsMap,
@@ -306,7 +313,7 @@ export class Client<
306313
* @param reason
307314
* @private
308315
*/
309-
private onclose(reason: string): void {
316+
private onclose(reason: CloseReason | "forced server close"): void {
310317
debug("client close with reason %s", reason);
311318

312319
// ignore a potential subsequent `close` event

lib/socket.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,23 @@ const debug = debugModule("socket.io:socket");
2525

2626
type ClientReservedEvents = "connect_error";
2727

28+
// TODO for next major release: cleanup disconnect reasons
29+
export type DisconnectReason =
30+
// Engine.IO close reasons
31+
| "transport error"
32+
| "transport close"
33+
| "forced close"
34+
| "ping timeout"
35+
| "parse error"
36+
// Socket.IO disconnect reasons
37+
| "server shutting down"
38+
| "forced server close"
39+
| "client namespace disconnect"
40+
| "server namespace disconnect";
41+
2842
export interface SocketReservedEventsMap {
29-
disconnect: (reason: string) => void;
30-
disconnecting: (reason: string) => void;
43+
disconnect: (reason: DisconnectReason) => void;
44+
disconnecting: (reason: DisconnectReason) => void;
3145
error: (err: Error) => void;
3246
}
3347

@@ -509,7 +523,7 @@ export class Socket<
509523
*
510524
* @private
511525
*/
512-
_onclose(reason: string): this | undefined {
526+
_onclose(reason: DisconnectReason): this | undefined {
513527
if (!this.connected) return this;
514528
debug("closing socket - reason %s", reason);
515529
this.emitReserved("disconnecting", reason);

test/socket.io.test-d.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { DefaultEventsMap } from "../lib/typed-events";
44
import { createServer } from "http";
55
import { expectError, expectType } from "tsd";
66
import { Adapter } from "socket.io-adapter";
7+
import type { DisconnectReason } from "../lib/socket";
78

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

@@ -17,10 +18,10 @@ describe("server", () => {
1718
sio.on("connection", (s) => {
1819
expectType<Socket<DefaultEventsMap, DefaultEventsMap>>(s);
1920
s.on("disconnect", (reason) => {
20-
expectType<string>(reason);
21+
expectType<DisconnectReason>(reason);
2122
});
2223
s.on("disconnecting", (reason) => {
23-
expectType<string>(reason);
24+
expectType<DisconnectReason>(reason);
2425
});
2526
});
2627
sio.on("connect", (s) => {

0 commit comments

Comments
 (0)