@@ -45,7 +45,8 @@ interface GobanSocketOptions {
45
45
/** Don't automatically send pings */
46
46
dont_ping ?: boolean ;
47
47
48
- ping_interval ?: number ; // milliseconds
48
+ // Note: you can't turn off ping by setting ping interval to zero or undefined.
49
+ ping_interval ?: number ; // milliseconds, applied if non-zero.
49
50
timeout_delay ?: number ;
50
51
51
52
/** Don't log connection/disconnect things*/
@@ -61,7 +62,7 @@ const RECONNECTION_INTERVALS = [
61
62
[ 250 , 750 ] , // if that doesn't work, keep trying in try again in 250-750ms intervals
62
63
] ;
63
64
64
- const PING_INTERVAL = 10000 ;
65
+ const DEFAULT_PING_INTERVAL = 10000 ;
65
66
66
67
export type DataArgument < Entry > = Entry extends ( ...args : infer A ) => void ? A [ 0 ] : never ;
67
68
export type ResponseType < Entry > = Entry extends ( ...args : any [ ] ) => infer R ? R : never ;
@@ -86,6 +87,8 @@ export class GobanSocket<
86
87
public readonly url : string ;
87
88
public clock_drift = 0.0 ;
88
89
public latency = 0.0 ;
90
+ public options : GobanSocketOptions ;
91
+
89
92
private socket : WebSocket ;
90
93
private last_request_id = 0 ;
91
94
private promises_in_flight : Map <
@@ -106,7 +109,7 @@ export class GobanSocket<
106
109
private callbacks : Map < number , ( data ?: any , error ?: ErrorResponse ) => void > = new Map ( ) ;
107
110
private authentication ?: DataArgument < SendProtocol [ "authenticate" ] > ;
108
111
private manually_disconnected = false ;
109
- public options : GobanSocketOptions ;
112
+ private current_ping_interval : number ;
110
113
111
114
constructor ( url : string , options : GobanSocketOptions = { } ) {
112
115
super ( ) ;
@@ -115,6 +118,8 @@ export class GobanSocket<
115
118
url = url . replace ( / ^ h t t p / , "ws" ) ;
116
119
117
120
this . url = url ;
121
+ this . current_ping_interval = options . ping_interval || DEFAULT_PING_INTERVAL ;
122
+
118
123
this . socket = this . connect ( ) ;
119
124
120
125
this . on ( "net/pong" , ( { client, server } : { client : number ; server : number } ) => {
@@ -148,32 +153,42 @@ export class GobanSocket<
148
153
this . emit ( "timeout" ) ;
149
154
} ;
150
155
151
- private startPing ( ) : void {
152
- if ( ! this . connected ) {
153
- throw new Error ( "GobanSocket not connected" ) ;
156
+ ping = ( ) => {
157
+ if ( this . options . dont_ping ) {
158
+ return ;
154
159
}
155
160
156
- const ping = ( ) => {
157
- if ( this . options . dont_ping ) {
158
- return ;
161
+ if ( this . connected ) {
162
+ this . send ( "net/ping" , {
163
+ client : Date . now ( ) ,
164
+ drift : this . clock_drift ,
165
+ latency : this . latency ,
166
+ } as DataArgument < SendProtocol [ "net/ping" ] > ) ;
167
+ if ( this . options . timeout_delay ) {
168
+ this . timeout_timer = setTimeout ( this . signalTimeout , this . options . timeout_delay ) ;
159
169
}
160
-
161
- if ( this . connected ) {
162
- this . send ( "net/ping" , {
163
- client : Date . now ( ) ,
164
- drift : this . clock_drift ,
165
- latency : this . latency ,
166
- } as DataArgument < SendProtocol [ "net/ping" ] > ) ;
167
- if ( this . options . timeout_delay ) {
168
- this . timeout_timer = setTimeout ( this . signalTimeout , this . options . timeout_delay ) ;
169
- }
170
- } else {
171
- if ( this . ping_timer ) {
172
- clearInterval ( this . ping_timer ) ;
173
- this . ping_timer = undefined ;
174
- }
170
+ if (
171
+ this . options . ping_interval &&
172
+ this . options . ping_interval !== this . current_ping_interval
173
+ ) {
174
+ clearInterval ( this . ping_timer ) ;
175
+ this . ping_timer = niceInterval (
176
+ this . ping ,
177
+ this . options . ping_interval || DEFAULT_PING_INTERVAL ,
178
+ ) ;
175
179
}
176
- } ;
180
+ } else {
181
+ if ( this . ping_timer ) {
182
+ clearInterval ( this . ping_timer ) ;
183
+ this . ping_timer = undefined ;
184
+ }
185
+ }
186
+ } ;
187
+
188
+ private startPing ( ) : void {
189
+ if ( ! this . connected ) {
190
+ throw new Error ( "GobanSocket not connected" ) ;
191
+ }
177
192
178
193
if ( this . ping_timer ) {
179
194
clearInterval ( this . ping_timer ) ;
@@ -182,8 +197,11 @@ export class GobanSocket<
182
197
clearTimeout ( this . timeout_timer ) ;
183
198
}
184
199
185
- this . ping_timer = niceInterval ( ping , this . options . ping_interval ?? PING_INTERVAL ) ;
186
- ping ( ) ;
200
+ this . ping_timer = niceInterval (
201
+ this . ping ,
202
+ this . options . ping_interval || DEFAULT_PING_INTERVAL ,
203
+ ) ;
204
+ this . ping ( ) ;
187
205
}
188
206
189
207
private connect ( ) : WebSocket {
0 commit comments