Skip to content

Commit 738e07f

Browse files
authored
[fix] Make WebSocket#terminate() destroy the socket (#1033)
1 parent 72170d4 commit 738e07f

File tree

2 files changed

+6
-17
lines changed

2 files changed

+6
-17
lines changed

doc/ws.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,11 +406,11 @@ Resume the socket
406406
- `callback` {Function} An optional callback which is invoked when `data` is
407407
written out.
408408

409-
Sends `data` through the connection.
409+
Send `data` through the connection.
410410

411411
### websocket.terminate()
412412

413-
Send a FIN packet to the other peer.
413+
Forcibly close the connection.
414414

415415
### websocket.upgradeReq
416416

lib/WebSocket.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -276,17 +276,16 @@ class WebSocket extends EventEmitter {
276276
}
277277

278278
if (this.readyState === WebSocket.CLOSING) {
279-
if (this._closeCode) this.terminate();
279+
if (this._closeCode && this._socket) this._socket.end();
280280
return;
281281
}
282282

283283
this.readyState = WebSocket.CLOSING;
284284
this._sender.close(code, data, !this._isServer, (err) => {
285285
if (err) this.emit('error', err);
286286

287-
if (this._closeCode) {
288-
this.terminate();
289-
} else {
287+
if (this._socket) {
288+
if (this._closeCode) this._socket.end();
290289
//
291290
// Ensure that the connection is cleaned up even when the closing
292291
// handshake fails.
@@ -391,17 +390,7 @@ class WebSocket extends EventEmitter {
391390
return;
392391
}
393392

394-
if (this._socket) {
395-
this.readyState = WebSocket.CLOSING;
396-
this._socket.end();
397-
398-
//
399-
// Add a timeout to ensure that the connection is completely cleaned up
400-
// within 30 seconds, even if the other peer does not send a FIN packet.
401-
//
402-
clearTimeout(this._closeTimer);
403-
this._closeTimer = setTimeout(this._finalize, closeTimeout, true);
404-
}
393+
this.finalize(true);
405394
}
406395
}
407396

0 commit comments

Comments
 (0)