Skip to content

Commit

Permalink
feat: listen to the "offline" event
Browse files Browse the repository at this point in the history
The connection will be closed once the "offline" event is emitted by
the browser, in order not to wait for the heartbeat mechanism to detect
the disconnection.

Reference: https://developer.mozilla.org/en-US/docs/Web/API/Window/offline_event

Related: socketio/socket.io-client#1433
  • Loading branch information
darrachequesne committed Mar 2, 2021
1 parent 6f7c89d commit c361bc6
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/socket.js
Expand Up @@ -102,6 +102,12 @@ class Socket extends Emitter {
},
false
);
if (this.hostname !== "localhost") {
this.offlineEventListener = () => {
this.onClose("transport close");
};
addEventListener("offline", this.offlineEventListener, false);
}
}

this.open();
Expand Down Expand Up @@ -622,6 +628,10 @@ class Socket extends Emitter {
// ignore further transport communication
this.transport.removeAllListeners();

if (typeof removeEventListener === "function") {
removeEventListener("offline", this.offlineEventListener, false);
}

// set ready state
this.readyState = "closed";

Expand Down

0 comments on commit c361bc6

Please sign in to comment.