Skip to content

Commit ed48b5d

Browse files
fix: silently close the transport in the beforeunload hook
Related: - socketio/socket.io#3639 - socketio/socket.io#3069
1 parent 76338d7 commit ed48b5d

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

‎lib/socket.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,20 @@ class Socket extends Emitter{
9090
// set on heartbeat
9191
this.pingTimeoutTimer=null;
9292

93+
if(typeofaddEventListener==="function"){
94+
addEventListener(
95+
"beforeunload",
96+
()=>{
97+
if(this.transport){
98+
// silently close the transport
99+
this.transport.removeAllListeners();
100+
this.transport.close();
101+
}
102+
},
103+
false
104+
);
105+
}
106+
93107
this.open();
94108
}
95109

‎lib/transports/polling-jsonp.js‎

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ const rEscapedNewline = /\\n/g;
1010

1111
letcallbacks;
1212

13-
/**
14-
* Noop.
15-
*/
16-
17-
functionempty(){}
18-
1913
classJSONPPollingextendsPolling{
2014
/**
2115
* JSONP Polling constructor.
@@ -46,17 +40,6 @@ class JSONPPolling extends Polling{
4640

4741
// append to query string
4842
this.query.j=this.index;
49-
50-
// prevent spurious errors from being emitted when the window is unloaded
51-
if(typeofaddEventListener==="function"){
52-
addEventListener(
53-
"beforeunload",
54-
function(){
55-
if(self.script)self.script.onerror=empty;
56-
},
57-
false
58-
);
59-
}
6043
}
6144

6245
/**
@@ -73,6 +56,8 @@ class JSONPPolling extends Polling{
7356
*/
7457
doClose(){
7558
if(this.script){
59+
// prevent spurious errors from being emitted when the window is unloaded
60+
this.script.onerror=()=>{};
7661
this.script.parentNode.removeChild(this.script);
7762
this.script=null;
7863
}

‎lib/transports/websocket.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ class WS extends Transport{
201201
doClose(){
202202
if(typeofthis.ws!=="undefined"){
203203
this.ws.close();
204+
this.ws=null;
204205
}
205206
}
206207

0 commit comments

Comments
(0)