Skip to content

Commit

Permalink
Prevent wsConnection from being undefined.
Browse files Browse the repository at this point in the history
If the handshake is bad, it could still be undefined so handle that in doClose.
  • Loading branch information
GICodeWarrior committed Aug 11, 2012
1 parent face934 commit 199b79e
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions lib/transports/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,28 +153,24 @@ WebSocket.prototype.onSocketConnect = function () {
return;
}

wsRequest.once('requestAccepted', function(wsConnection) {
self.wsConnection = wsConnection;
self.flush();

wsConnection.on('message', function(message) {
if (message.type === 'utf8') {
self.onMessage(parser.decodePacket(message.utf8Data));
}
else if (message.type === 'binary') {
self.log.warn(self.name + ' unsupported websocket binary message received.');
}
});
wsConnection.on('close', function() {
self.onClose();
});
wsConnection.on('error', function(error) {
self.log.warn(self.name + ' connection error: ' + error);
});
});

// TODO: Make the cross-origin and protocol handling secure
wsRequest.accept(wsRequest.requestedProtocols[0], wsRequest.origin);
this.wsConnection = wsRequest.accept(wsRequest.requestedProtocols[0], wsRequest.origin);
this.flush();

this.wsConnection.on('message', function(message) {
if (message.type === 'utf8') {
self.onMessage(parser.decodePacket(message.utf8Data));
}
else if (message.type === 'binary') {
self.log.warn(self.name + ' unsupported websocket binary message received.');
}
});
this.wsConnection.on('close', function() {
self.onClose();
});
this.wsConnection.on('error', function(error) {
self.log.warn(self.name + ' connection error: ' + error);
});
};

/**
Expand Down Expand Up @@ -235,5 +231,7 @@ WebSocket.prototype.payload = function (msgs) {
*/

WebSocket.prototype.doClose = function () {
this.wsConnection.close();
if (this.wsConnection) {
this.wsConnection.close();
}
};

0 comments on commit 199b79e

Please sign in to comment.