File tree 3 files changed +28
-6
lines changed
3 files changed +28
-6
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,13 @@ interface WriteOptions {
18
18
wsPreEncoded ?: string ;
19
19
}
20
20
21
+ type CloseReason =
22
+ | "transport error"
23
+ | "transport close"
24
+ | "forced close"
25
+ | "ping timeout"
26
+ | "parse error" ;
27
+
21
28
export class Client <
22
29
ListenEvents extends EventsMap ,
23
30
EmitEvents extends EventsMap ,
@@ -306,7 +313,7 @@ export class Client<
306
313
* @param reason
307
314
* @private
308
315
*/
309
- private onclose ( reason : string ) : void {
316
+ private onclose ( reason : CloseReason | "forced server close" ) : void {
310
317
debug ( "client close with reason %s" , reason ) ;
311
318
312
319
// 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");
25
25
26
26
type ClientReservedEvents = "connect_error" ;
27
27
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
+
28
42
export interface SocketReservedEventsMap {
29
- disconnect : ( reason : string ) => void ;
30
- disconnecting : ( reason : string ) => void ;
43
+ disconnect : ( reason : DisconnectReason ) => void ;
44
+ disconnecting : ( reason : DisconnectReason ) => void ;
31
45
error : ( err : Error ) => void ;
32
46
}
33
47
@@ -509,7 +523,7 @@ export class Socket<
509
523
*
510
524
* @private
511
525
*/
512
- _onclose ( reason : string ) : this | undefined {
526
+ _onclose ( reason : DisconnectReason ) : this | undefined {
513
527
if ( ! this . connected ) return this ;
514
528
debug ( "closing socket - reason %s" , reason ) ;
515
529
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";
4
4
import { createServer } from "http" ;
5
5
import { expectError , expectType } from "tsd" ;
6
6
import { Adapter } from "socket.io-adapter" ;
7
+ import type { DisconnectReason } from "../lib/socket" ;
7
8
8
9
// This file is run by tsd, not mocha.
9
10
@@ -17,10 +18,10 @@ describe("server", () => {
17
18
sio . on ( "connection" , ( s ) => {
18
19
expectType < Socket < DefaultEventsMap , DefaultEventsMap > > ( s ) ;
19
20
s . on ( "disconnect" , ( reason ) => {
20
- expectType < string > ( reason ) ;
21
+ expectType < DisconnectReason > ( reason ) ;
21
22
} ) ;
22
23
s . on ( "disconnecting" , ( reason ) => {
23
- expectType < string > ( reason ) ;
24
+ expectType < DisconnectReason > ( reason ) ;
24
25
} ) ;
25
26
} ) ;
26
27
sio . on ( "connect" , ( s ) => {
You can’t perform that action at this time.
0 commit comments