Skip to content

Commit

Permalink
Allow multiple connections to simultaneously reconnect
Browse files Browse the repository at this point in the history
- This affects the webSockets and serverSentEvents transports

#1243
  • Loading branch information
halter73 committed Feb 13, 2013
1 parent 1d78c0f commit 9ce2f81
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 32 deletions.
Expand Up @@ -16,10 +16,6 @@

supportsKeepAlive: true,

reconnectTimeout: false,

currentEventSourceID: 0,

timeOut: 3000,

start: function (connection, onSuccess, onFailed) {
Expand Down Expand Up @@ -48,7 +44,6 @@
try {
connection.log("Attempting to connect to SSE endpoint '" + url + "'");
connection.eventSource = new window.EventSource(url);
connection.eventSource.ID = ++that.currentEventSourceID;
}
catch (e) {
connection.log("EventSource failed trying to connect with error " + e.Message);
Expand Down Expand Up @@ -99,8 +94,9 @@
window.clearTimeout(connectTimeOut);
}

if (that.reconnectTimeout) {
window.clearTimeout(that.reconnectTimeout);
if (connection.reconnectTimeout) {
window.clearTimeout(connection.reconnectTimeout);
delete connection.reconnectTimeout;
}

if (opened === false) {
Expand Down Expand Up @@ -130,7 +126,7 @@
// Only handle an error if the error is from the current Event Source.
// Sometimes on disconnect the server will push down an error event
// to an expired Event Source.
if (this.ID === that.currentEventSourceID) {
if (this === connection.eventSource) {
if (!opened) {
if (onFailed) {
onFailed();
Expand Down Expand Up @@ -160,14 +156,16 @@
reconnect: function (connection) {
var that = this;

that.reconnectTimeout = window.setTimeout(function () {
that.stop(connection);
if (connection.state !== signalR.connectionState.disconnected) {
connection.reconnectTimeout = window.setTimeout(function () {
that.stop(connection);

if (transportLogic.ensureReconnectingState(connection)) {
connection.log("EventSource reconnecting");
that.start(connection);
}
}, connection.reconnectDelay);
if (transportLogic.ensureReconnectingState(connection)) {
connection.log("EventSource reconnecting");
that.start(connection);
}
}, connection.reconnectDelay);
}
},

lostConnection: function (connection) {
Expand All @@ -181,12 +179,12 @@
stop: function (connection) {
if (connection && connection.eventSource) {
connection.log("EventSource calling close()");
connection.eventSource.ID = null;
connection.eventSource.close();
connection.eventSource = null;
delete connection.eventSource;
}
},

abort: function (connection, async) {
transportLogic.ajaxAbort(connection, async);
}
Expand Down
Expand Up @@ -16,10 +16,6 @@

supportsKeepAlive: true,

attemptingReconnect: false,

currentSocketID: 0,

send: function (connection, data) {
connection.socket.send(data);
},
Expand Down Expand Up @@ -48,13 +44,13 @@

connection.log("Connecting to websocket endpoint '" + url + "'");
connection.socket = new window.WebSocket(url);
connection.socket.ID = ++that.currentSocketID;
connection.socket.onopen = function () {
opened = true;
connection.log("Websocket opened");

if (that.attemptingReconnect) {
that.attemptingReconnect = false;
if (connection.reconnectTimeout) {
window.clearTimeout(connection.reconnectTimeout);
delete connection.reconnectTimeout;
}

if (onSuccess) {
Expand All @@ -70,7 +66,7 @@
// Only handle a socket close if the close is from the current socket.
// Sometimes on disconnect the server will push down an onclose event
// to an expired socket.
if (this.ID === that.currentSocketID) {
if (this === connection.socket) {
if (!opened) {
if (onFailed) {
onFailed();
Expand Down Expand Up @@ -116,14 +112,8 @@
var that = this;

if (connection.state !== signalR.connectionState.disconnected) {
if (!that.attemptingReconnect) {
that.attemptingReconnect = true;
}

window.setTimeout(function () {
if (that.attemptingReconnect) {
that.stop(connection);
}
connection.reconnectTimeout = window.setTimeout(function () {
that.stop(connection);

if (transportLogic.ensureReconnectingState(connection)) {
connection.log("Websocket reconnecting");
Expand Down

0 comments on commit 9ce2f81

Please sign in to comment.