Permalink
Browse files

Allow closing connections in the CONNECTING state.

- Also, explicitly close the HTTP client instance if we fail to connect.
  • Loading branch information...
1 parent c904fdf commit bf4f9d758a222c07ac9c55aa6d3bfb7e531cf702 @pgriess committed Sep 23, 2010
Showing with 24 additions and 19 deletions.
  1. +24 −19 lib/websocket.js
View
@@ -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
@@ -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);

0 comments on commit bf4f9d7

Please sign in to comment.