Skip to content

Make WebSocket#terminate() destroy the socket #1033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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