Skip to content

Commit

Permalink
[fix] Make WebSocket#terminate() destroy the socket (#1033)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca authored Mar 11, 2017
1 parent 72170d4 commit 738e07f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
4 changes: 2 additions & 2 deletions doc/ws.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 4 additions & 15 deletions lib/WebSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,17 +276,16 @@ class WebSocket extends EventEmitter {
}

if (this.readyState === WebSocket.CLOSING) {
if (this._closeCode) this.terminate();
if (this._closeCode && this._socket) this._socket.end();
return;
}

this.readyState = WebSocket.CLOSING;
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.
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 738e07f

Please sign in to comment.