Skip to content

Commit 81d7171

Browse files
feat: reverse the ping-pong mechanism
The ping packets will now be sent by the server, because the timers set in the browsers are not reliable enough. We suspect that a lot of timeout problems came from timers being delayed on the client-side. Breaking change: v3.x clients will not be able to connect anymore (they will send a ping packet and timeout while waiting for a pong packet). Related: https://github.com/socketio/engine.io/issues/312
1 parent 581ceff commit 81d7171

File tree

2 files changed

+11
-48
lines changed

2 files changed

+11
-48
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ Exposed as `eio` in the browser standalone build.
172172
- `upgrade`
173173
- Fired upon upgrade success, after the new transport is set
174174
- `ping`
175-
- Fired upon _flushing_ a ping packet (ie: actual packet write out)
175+
- Fired upon receiving a ping packet.
176176
- `pong`
177-
- Fired upon receiving a pong packet.
177+
- Fired upon _flushing_ a pong packet (ie: actual packet write out)
178178
179179
#### Methods
180180

lib/socket.js

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ class Socket extends Emitter {
116116
this.pingTimeout = null;
117117

118118
// set on heartbeat
119-
this.pingIntervalTimer = null;
120119
this.pingTimeoutTimer = null;
121120

122121
this.open();
@@ -415,8 +414,9 @@ class Socket extends Emitter {
415414
this.onHandshake(JSON.parse(packet.data));
416415
break;
417416

418-
case "pong":
419-
this.setPing();
417+
case "ping":
418+
this.resetPingTimeout();
419+
this.sendPacket("pong");
420420
this.emit("pong");
421421
break;
422422

@@ -452,56 +452,19 @@ class Socket extends Emitter {
452452
this.onOpen();
453453
// In case open handler closes socket
454454
if ("closed" === this.readyState) return;
455-
this.setPing();
456-
457-
// Prolong liveness of socket on heartbeat
458-
this.removeListener("heartbeat", this.onHeartbeat);
459-
this.on("heartbeat", this.onHeartbeat);
455+
this.resetPingTimeout();
460456
}
461457

462458
/**
463-
* Resets ping timeout.
459+
* Sets and resets ping timeout timer based on server pings.
464460
*
465461
* @api private
466462
*/
467-
onHeartbeat(timeout) {
463+
resetPingTimeout() {
468464
clearTimeout(this.pingTimeoutTimer);
469-
const self = this;
470-
self.pingTimeoutTimer = setTimeout(function() {
471-
if ("closed" === self.readyState) return;
472-
self.onClose("ping timeout");
473-
}, timeout || self.pingInterval + self.pingTimeout);
474-
}
475-
476-
/**
477-
* Pings server every `this.pingInterval` and expects response
478-
* within `this.pingTimeout` or closes connection.
479-
*
480-
* @api private
481-
*/
482-
setPing() {
483-
const self = this;
484-
clearTimeout(self.pingIntervalTimer);
485-
self.pingIntervalTimer = setTimeout(function() {
486-
debug(
487-
"writing ping packet - expecting pong within %sms",
488-
self.pingTimeout
489-
);
490-
self.ping();
491-
self.onHeartbeat(self.pingTimeout);
492-
}, self.pingInterval);
493-
}
494-
495-
/**
496-
* Sends a ping packet.
497-
*
498-
* @api private
499-
*/
500-
ping() {
501-
const self = this;
502-
this.sendPacket("ping", function() {
503-
self.emit("ping");
504-
});
465+
this.pingTimeoutTimer = setTimeout(() => {
466+
this.onClose("ping timeout");
467+
}, this.pingInterval + this.pingTimeout);
505468
}
506469

507470
/**

0 commit comments

Comments
 (0)