File tree Expand file tree Collapse file tree 3 files changed +28
-6
lines changed Expand file tree Collapse file tree 3 files changed +28
-6
lines changed Original file line number Diff line number Diff 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+
2128export 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
Original file line number Diff line number Diff line change @@ -25,9 +25,23 @@ const debug = debugModule("socket.io:socket");
2525
2626type 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+
2842export 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 ) ;
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import type { DefaultEventsMap } from "../lib/typed-events";
44import { createServer } from "http" ;
55import { expectError , expectType } from "tsd" ;
66import { 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 ) => {
You can’t perform that action at this time.
0 commit comments