Skip to content

Commit fc0d15b

Browse files
committed
emit both an error and close event when connection establishment fails
1 parent 1242a8c commit fc0d15b

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

lib/WebSocket.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -936,21 +936,18 @@ function sendStream(instance, stream, options, cb) {
936936
function cleanupWebsocketResources(error) {
937937
if (this.readyState === WebSocket.CLOSED) return;
938938

939-
var emitClose = this.readyState !== WebSocket.CONNECTING;
940939
this.readyState = WebSocket.CLOSED;
941940

942941
clearTimeout(this._closeTimer);
943942
this._closeTimer = null;
944943

945-
if (emitClose) {
946-
// If the connection was closed abnormally (with an error), or if
947-
// the close control frame was not received then the close code
948-
// must default to 1006.
949-
if (error || !this._closeReceived) {
950-
this._closeCode = 1006;
951-
}
952-
this.emit('close', this._closeCode || 1000, this._closeMessage || '');
944+
// If the connection was closed abnormally (with an error), or if
945+
// the close control frame was not received then the close code
946+
// must default to 1006.
947+
if (error || !this._closeReceived) {
948+
this._closeCode = 1006;
953949
}
950+
this.emit('close', this._closeCode || 1000, this._closeMessage || '');
954951

955952
if (this._socket) {
956953
if (this._ultron) this._ultron.destroy();

test/WebSocket.test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,11 +398,13 @@ describe('WebSocket', function() {
398398
ws.on('open', function() {
399399
assert.fail('connect shouldnt be raised here');
400400
});
401-
ws.on('close', function() {
402-
assert.fail('close shouldnt be raised here');
403-
});
401+
var errorCallBackFired = false;
404402
ws.on('error', function() {
403+
errorCallBackFired = true;
404+
});
405+
ws.on('close', function() {
405406
setTimeout(function() {
407+
assert.equal(true, errorCallBackFired);
406408
assert.equal(ws.readyState, WebSocket.CLOSED);
407409
done();
408410
}, 50)

0 commit comments

Comments
 (0)