Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var SockJS;
* @param {String} url
* @constructor
*/
function Client(url) {
function Client(url, retryAttempts) {
/** @type {String} */
this._url = url;
/** @type {SockJS} */
Expand All @@ -19,8 +19,18 @@ function Client(url) {
this._heartbeatTimeout = null;
/** @type {Number} */
this._reopenTimeout = null;
/** @type {Number} */
this._retryAttempts = retryAttempts || 6;
/** @type {Number} */
this._counter = 0;
/** @type {Function} */
this._errorCallback = null;
}

Client.prototype.onCaptureError = function(callback) {
this._errorCallback = callback;
};

Client.prototype.open = function() {
clearTimeout(this._reopenTimeout);
this._reopenTimeout = null;
Expand All @@ -39,8 +49,15 @@ Client.prototype._reopen = function() {
this._sockJS = null;

this._reopenTimeout = setTimeout(function() {
this.open();
}.bind(this), 1000);
if (this._counter < this._retryAttempts) {
this._counter++;
this.open();
} else {
if (typeof this._errorCallback === 'function') {
this._errorCallback(new Error('Reached max retryAttempts'));
}
}
}.bind(this), 1000 * this._counter);
};

Client.prototype._onopen = function() {
Expand All @@ -49,6 +66,9 @@ Client.prototype._onopen = function() {
self._subscribe(channel, self._closeStamp);
});

/** Reset counter if connected. */
this._counter = 0;

this._closeStamp = null;
this._sockJS.onmessage = function(event) {
var data = JSON.parse(event.data);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "socket-redis",
"version": "3.3.1",
"version": "3.3.4",
"description": "Redis to SockJS relay",
"main": "socket-redis.js",
"bin": {
Expand Down
Loading