@@ -30,6 +30,7 @@ class Socket extends EventEmitter {
30
30
this . checkIntervalTimer = null ;
31
31
this . upgradeTimeoutTimer = null ;
32
32
this . pingTimeoutTimer = null ;
33
+ this . pingIntervalTimer = null ;
33
34
34
35
this . setTransport ( transport ) ;
35
36
this . onOpen ( ) ;
@@ -60,7 +61,7 @@ class Socket extends EventEmitter {
60
61
}
61
62
62
63
this . emit ( "open" ) ;
63
- this . setPingTimeout ( ) ;
64
+ this . schedulePing ( ) ;
64
65
}
65
66
66
67
/**
@@ -77,12 +78,12 @@ class Socket extends EventEmitter {
77
78
78
79
// Reset ping timeout on any packet, incoming data is a good sign of
79
80
// other side's liveness
80
- this . setPingTimeout ( ) ;
81
+ this . resetPingTimeout ( this . server . pingInterval + this . server . pingTimeout ) ;
81
82
82
83
switch ( packet . type ) {
83
- case "ping " :
84
- debug ( "got ping " ) ;
85
- this . sendPacket ( "pong" ) ;
84
+ case "pong " :
85
+ debug ( "got pong " ) ;
86
+ this . schedulePing ( ) ;
86
87
this . emit ( "heartbeat" ) ;
87
88
break ;
88
89
@@ -112,15 +113,34 @@ class Socket extends EventEmitter {
112
113
}
113
114
114
115
/**
115
- * Sets and resets ping timeout timer based on client pings.
116
+ * Pings client every `this.pingInterval` and expects response
117
+ * within `this.pingTimeout` or closes connection.
116
118
*
117
119
* @api private
118
120
*/
119
- setPingTimeout ( ) {
121
+ schedulePing ( ) {
122
+ clearTimeout ( this . pingIntervalTimer ) ;
123
+ this . pingIntervalTimer = setTimeout ( ( ) => {
124
+ debug (
125
+ "writing ping packet - expecting pong within %sms" ,
126
+ this . server . pingTimeout
127
+ ) ;
128
+ this . sendPacket ( "ping" ) ;
129
+ this . resetPingTimeout ( this . server . pingTimeout ) ;
130
+ } , this . server . pingInterval ) ;
131
+ }
132
+
133
+ /**
134
+ * Resets ping timeout.
135
+ *
136
+ * @api private
137
+ */
138
+ resetPingTimeout ( timeout ) {
120
139
clearTimeout ( this . pingTimeoutTimer ) ;
121
140
this . pingTimeoutTimer = setTimeout ( ( ) => {
141
+ if ( this . readyState === "closed" ) return ;
122
142
this . onClose ( "ping timeout" ) ;
123
- } , this . server . pingInterval + this . server . pingTimeout ) ;
143
+ } , timeout ) ;
124
144
}
125
145
126
146
/**
@@ -191,7 +211,7 @@ class Socket extends EventEmitter {
191
211
self . clearTransport ( ) ;
192
212
self . setTransport ( transport ) ;
193
213
self . emit ( "upgrade" , transport ) ;
194
- self . setPingTimeout ( ) ;
214
+ self . schedulePing ( ) ;
195
215
self . flush ( ) ;
196
216
if ( self . readyState === "closing" ) {
197
217
transport . close ( function ( ) {
@@ -283,7 +303,11 @@ class Socket extends EventEmitter {
283
303
onClose ( reason , description ) {
284
304
if ( "closed" !== this . readyState ) {
285
305
this . readyState = "closed" ;
306
+
307
+ // clear timers
308
+ clearTimeout ( this . pingIntervalTimer ) ;
286
309
clearTimeout ( this . pingTimeoutTimer ) ;
310
+
287
311
clearInterval ( this . checkIntervalTimer ) ;
288
312
this . checkIntervalTimer = null ;
289
313
clearTimeout ( this . upgradeTimeoutTimer ) ;
0 commit comments