Skip to content

Commit

Permalink
Allow closing connections in the CONNECTING state.
Browse files Browse the repository at this point in the history
- Also, explicitly close the HTTP client instance if we fail to connect.
  • Loading branch information
pgriess committed Sep 23, 2010
1 parent c904fdf commit bf4f9d7
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions lib/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,34 +331,36 @@ var WebSocket = function(url, proto, opts) {

// External API
self.close = function() {
var f = function() {
readyState = CLOSED;

if (stream) {
stream.end();
stream.destroy();
stream = undefined;
}

process.nextTick(function() {
self.emit('close');

if (self.onclose) {
self.onclose();
}
});
};

switch (readyState) {
case CLOSED:
case CLOSING:
break;

case CONNECTING:
throw new Error(
'Closing of an CONNECTING socket not yet implemented'
);
f();
break;

default:
readyState = CLOSING;

var f = function() {
stream.end();
stream.destroy();
stream = undefined;
readyState = CLOSED;

process.nextTick(function() {
self.emit('close');

if (self.onclose) {
self.onclose();
}
});
};

// XXX: Run f() on the next tick so that we conform a little
// closer to the spirit of the API in that the caller
// never sees us transition directly to CLOSED. Instead, we
Expand Down Expand Up @@ -537,7 +539,10 @@ var WebSocket = function(url, proto, opts) {
stream.emit('data', head);
};
})());
httpClient.addListener('error', errorListener);
httpClient.addListener('error', function(e) {
httpClient.end();
errorListener(e);
});

var httpReq = httpClient.request(httpPath, httpHeaders);

Expand Down

0 comments on commit bf4f9d7

Please sign in to comment.