Skip to content

Commit

Permalink
v6.1.0 - Abort all pending events when the socket becomes disconnecte…
Browse files Browse the repository at this point in the history
…d or the connection is aborted
  • Loading branch information
jondubois committed Jul 8, 2017
1 parent f7f33d7 commit 205ef89
Show file tree
Hide file tree
Showing 7 changed files with 828 additions and 715 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "socketcluster-client",
"main": "socketcluster.js",
"version": "5.5.2",
"version": "6.1.0",
"homepage": "https://github.com/SocketCluster/socketcluster-client",
"description": "SocketCluster JavaScript client",
"authors": [
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ module.exports.destroy = function (options) {

module.exports.connections = SCSocketCreator.connections;

module.exports.version = '5.5.2';
module.exports.version = '6.1.0';
26 changes: 26 additions & 0 deletions lib/scsocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var InvalidArgumentsError = scErrors.InvalidArgumentsError;
var InvalidMessageError = scErrors.InvalidMessageError;
var SocketProtocolError = scErrors.SocketProtocolError;
var TimeoutError = scErrors.TimeoutError;
var BadConnectionError = scErrors.BadConnectionError;

var isBrowser = typeof window != 'undefined';

Expand Down Expand Up @@ -545,6 +546,29 @@ SCSocket.prototype._suspendSubscriptions = function () {
}
};

SCSocket.prototype._abortAllPendingEventsDueToBadConnection = function (failureType) {
var currentNode = this._emitBuffer.head;
var nextNode;

while (currentNode) {
nextNode = currentNode.next;
var eventObject = currentNode.data;
clearTimeout(eventObject.timeout);
delete eventObject.timeout;
currentNode.detach();
currentNode = nextNode;

var callback = eventObject.callback;
if (callback) {
delete eventObject.callback;
var errorMessage = "Event '" + eventObject.event +
"' was aborted due to a bad connection";
var error = new BadConnectionError(errorMessage, failureType);
callback.call(eventObject, error, eventObject);
}
}
};

SCSocket.prototype._onSCClose = function (code, data, openAbort) {
var self = this;

Expand Down Expand Up @@ -597,6 +621,8 @@ SCSocket.prototype._onSCClose = function (code, data, openAbort) {
var err = new SocketProtocolError(SCSocket.errorStatuses[code] || failureMessage, code);
this._onSCError(err);
}

this._abortAllPendingEventsDueToBadConnection(openAbort ? 'connectAbort' : 'disconnect');
};

SCSocket.prototype._onSCEvent = function (event, data, res) {
Expand Down
24 changes: 24 additions & 0 deletions lib/sctransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ if (global.WebSocket) {

var scErrors = require('sc-errors');
var TimeoutError = scErrors.TimeoutError;
var BadConnectionError = scErrors.BadConnectionError;


var SCTransport = function (authEngine, codecEngine, options) {
Expand Down Expand Up @@ -168,6 +169,26 @@ SCTransport.prototype._handshake = function (callback) {
});
};

SCTransport.prototype._abortAllPendingEventsDueToBadConnection = function (failureType) {
for (var i in this._callbackMap) {
if (this._callbackMap.hasOwnProperty(i)) {
var eventObject = this._callbackMap[i];
delete this._callbackMap[i];

clearTimeout(eventObject.timeout);
delete eventObject.timeout;

var errorMessage = "Event '" + eventObject.event +
"' was aborted due to a bad connection";
var badConnectionError = new BadConnectionError(errorMessage, failureType);

var callback = eventObject.callback;
delete eventObject.callback;
callback.call(eventObject, badConnectionError, eventObject);
}
}
};

SCTransport.prototype._onClose = function (code, data) {
delete this.socket.onopen;
delete this.socket.onclose;
Expand All @@ -179,10 +200,12 @@ SCTransport.prototype._onClose = function (code, data) {
if (this.state == this.OPEN) {
this.state = this.CLOSED;
SCEmitter.prototype.emit.call(this, 'close', code, data);
this._abortAllPendingEventsDueToBadConnection('disconnect');

} else if (this.state == this.CONNECTING) {
this.state = this.CLOSED;
SCEmitter.prototype.emit.call(this, 'openAbort', code, data);
this._abortAllPendingEventsDueToBadConnection('connectAbort');
}
};

Expand All @@ -208,6 +231,7 @@ SCTransport.prototype._onMessage = function (message) {
var eventObject = this._callbackMap[obj.rid];
if (eventObject) {
clearTimeout(eventObject.timeout);
delete eventObject.timeout;
delete this._callbackMap[obj.rid];

if (eventObject.callback) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "socketcluster-client",
"description": "SocketCluster JavaScript client",
"version": "5.5.2",
"version": "6.1.0",
"homepage": "http://socketcluster.io",
"contributors": [
{
Expand All @@ -23,7 +23,7 @@
"querystring": "0.2.0",
"sc-channel": "~1.0.6",
"sc-emitter": "~1.1.0",
"sc-errors": "~1.3.0",
"sc-errors": "~1.3.2",
"sc-formatter": "~3.0.0",
"ws": "3.0.0"
},
Expand Down
Loading

0 comments on commit 205ef89

Please sign in to comment.