Skip to content

Commit c361bc6

Browse files
feat: listen to the "offline" event
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
1 parent 6f7c89d commit c361bc6

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

lib/socket.js

+10
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ class Socket extends Emitter {
102102
},
103103
false
104104
);
105+
if (this.hostname !== "localhost") {
106+
this.offlineEventListener = () => {
107+
this.onClose("transport close");
108+
};
109+
addEventListener("offline", this.offlineEventListener, false);
110+
}
105111
}
106112

107113
this.open();
@@ -622,6 +628,10 @@ class Socket extends Emitter {
622628
// ignore further transport communication
623629
this.transport.removeAllListeners();
624630

631+
if (typeof removeEventListener === "function") {
632+
removeEventListener("offline", this.offlineEventListener, false);
633+
}
634+
625635
// set ready state
626636
this.readyState = "closed";
627637

0 commit comments

Comments
 (0)