From 738e07f337fd601ba8ab0c0afc50f79296920fc8 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Sat, 11 Mar 2017 15:06:19 +0100 Subject: [PATCH] [fix] Make `WebSocket#terminate()` destroy the socket (#1033) --- doc/ws.md | 4 ++-- lib/WebSocket.js | 19 ++++--------------- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/doc/ws.md b/doc/ws.md index 3cc6c0a9e..8893edf47 100644 --- a/doc/ws.md +++ b/doc/ws.md @@ -406,11 +406,11 @@ Resume the socket - `callback` {Function} An optional callback which is invoked when `data` is written out. -Sends `data` through the connection. +Send `data` through the connection. ### websocket.terminate() -Send a FIN packet to the other peer. +Forcibly close the connection. ### websocket.upgradeReq diff --git a/lib/WebSocket.js b/lib/WebSocket.js index 0f8a2d3ae..c4ed744d5 100644 --- a/lib/WebSocket.js +++ b/lib/WebSocket.js @@ -276,7 +276,7 @@ class WebSocket extends EventEmitter { } if (this.readyState === WebSocket.CLOSING) { - if (this._closeCode) this.terminate(); + if (this._closeCode && this._socket) this._socket.end(); return; } @@ -284,9 +284,8 @@ class WebSocket extends EventEmitter { this._sender.close(code, data, !this._isServer, (err) => { if (err) this.emit('error', err); - if (this._closeCode) { - this.terminate(); - } else { + if (this._socket) { + if (this._closeCode) this._socket.end(); // // Ensure that the connection is cleaned up even when the closing // handshake fails. @@ -391,17 +390,7 @@ class WebSocket extends EventEmitter { return; } - if (this._socket) { - this.readyState = WebSocket.CLOSING; - this._socket.end(); - - // - // Add a timeout to ensure that the connection is completely cleaned up - // within 30 seconds, even if the other peer does not send a FIN packet. - // - clearTimeout(this._closeTimer); - this._closeTimer = setTimeout(this._finalize, closeTimeout, true); - } + this.finalize(true); } }