Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull request for issue 53, do not reconnect when the connection was closed by the client #54

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 15 additions & 13 deletions reconnecting-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
*
* Parameters
* ==========
* url - The url you are connecting to.
* url - The url you are connecting to, or a function to provide the function.
* protocols - Optional string or array of protocols.
* options - See below
*
Expand Down Expand Up @@ -143,8 +143,8 @@

// These should be treated as read-only properties

/** The URL as resolved by the constructor. This is always an absolute URL. Read only. */
this.url = url;
/** The URL as resolved by the constructor, or a function to return the current url. This is always an absolute URL. Read only. */
this.getUrl = typeof(url) === 'function' ? url : function() { return url; }

/** The number of attempted reconnects since starting, or the last successful connection. Read only. */
this.reconnectAttempts = 0;
Expand Down Expand Up @@ -203,7 +203,7 @@
};

this.open = function (reconnectAttempt) {
ws = new WebSocket(self.url, protocols || []);
ws = new WebSocket(self.getUrl(), protocols || []);

if (reconnectAttempt) {
if (this.maxReconnectAttempts && this.reconnectAttempts > this.maxReconnectAttempts) {
Expand All @@ -215,13 +215,13 @@
}

if (self.debug || ReconnectingWebSocket.debugAll) {
console.debug('ReconnectingWebSocket', 'attempt-connect', self.url);
console.debug('ReconnectingWebSocket', 'attempt-connect', self.getUrl());
}

var localWs = ws;
var timeout = setTimeout(function() {
if (self.debug || ReconnectingWebSocket.debugAll) {
console.debug('ReconnectingWebSocket', 'connection-timeout', self.url);
console.debug('ReconnectingWebSocket', 'connection-timeout', self.getUrl());
}
timedOut = true;
localWs.close();
Expand All @@ -231,7 +231,7 @@
ws.onopen = function(event) {
clearTimeout(timeout);
if (self.debug || ReconnectingWebSocket.debugAll) {
console.debug('ReconnectingWebSocket', 'onopen', self.url);
console.debug('ReconnectingWebSocket', 'onopen', self.getUrl());
}
self.protocol = ws.protocol;
self.readyState = WebSocket.OPEN;
Expand All @@ -257,29 +257,31 @@
eventTarget.dispatchEvent(e);
if (!reconnectAttempt && !timedOut) {
if (self.debug || ReconnectingWebSocket.debugAll) {
console.debug('ReconnectingWebSocket', 'onclose', self.url);
console.debug('ReconnectingWebSocket', 'onclose', self.getUrl());
}
eventTarget.dispatchEvent(generateEvent('close'));
}

var timeout = self.reconnectInterval * Math.pow(self.reconnectDecay, self.reconnectAttempts);
setTimeout(function() {
self.reconnectAttempts++;
self.open(true);
if (!forcedClose) {
self.reconnectAttempts++;
self.open(true);
}
}, timeout > self.maxReconnectInterval ? self.maxReconnectInterval : timeout);
}
};
ws.onmessage = function(event) {
if (self.debug || ReconnectingWebSocket.debugAll) {
console.debug('ReconnectingWebSocket', 'onmessage', self.url, event.data);
console.debug('ReconnectingWebSocket', 'onmessage', self.getUrl(), event.data);
}
var e = generateEvent('message');
e.data = event.data;
eventTarget.dispatchEvent(e);
};
ws.onerror = function(event) {
if (self.debug || ReconnectingWebSocket.debugAll) {
console.debug('ReconnectingWebSocket', 'onerror', self.url, event);
console.debug('ReconnectingWebSocket', 'onerror', self.getUrl(), event);
}
eventTarget.dispatchEvent(generateEvent('error'));
};
Expand All @@ -298,7 +300,7 @@
this.send = function(data) {
if (ws) {
if (self.debug || ReconnectingWebSocket.debugAll) {
console.debug('ReconnectingWebSocket', 'send', self.url, data);
console.debug('ReconnectingWebSocket', 'send', self.getUrl(), data);
}
return ws.send(data);
} else {
Expand Down