Skip to content

Commit

Permalink
socket,ws: use socket.destroy() properly
Browse files Browse the repository at this point in the history
`socket.destroy()` should only be called in case of application errors
that make it impossible to continue working with this socket (like
protocol parsing errors, exactly our situation). This method emits
`error` event on socket automatically if an exception is passed as the
first argument.

PR-URL: #84
  • Loading branch information
aqrln committed Mar 27, 2017
1 parent d8ba3fe commit 129e50f
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 12 deletions.
4 changes: 1 addition & 3 deletions lib/transport.socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,6 @@ SocketTransport.prototype.end = function(data) {
} else {
this.socket.end();
}

this.socket.destroy();
};

// Socket data handler
Expand All @@ -222,7 +220,7 @@ SocketTransport.prototype._onSocketData = function(chunk) {
try {
this._buffer = jsrs.parseNetworkPackets(this._buffer, packets);
} catch (error) {
this.emit('error', error);
this.socket.destroy(error);
return;
}

Expand Down
10 changes: 1 addition & 9 deletions lib/transport.ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ function JstpWebSocketServer(httpServer, config,
'listening',
'close'
]);

this.httpServer.on('clientError', this._onSocketError.bind(this));
common.forwardEvent(httpServer, this, 'clientError', 'error');

this.wsServer = new WebSocketServer(this.config);
this.wsServer.on('request', this._onRequest.bind(this));
Expand Down Expand Up @@ -147,13 +146,6 @@ JstpWebSocketServer.prototype._onRequest = function(request) {
this.emit('connection', connection);
};

// Socket error handler
//
JstpWebSocketServer.prototype._onSocketError = function(error, socket) {
socket.destroy();
this.emit('error', error);
};

// Default originCheckStrategy value for ws.createClient and
// JstpWebSocketServer constructor
//
Expand Down

0 comments on commit 129e50f

Please sign in to comment.