Skip to content

Commit

Permalink
Merge pull request #668 from peuter/fix-sse
Browse files Browse the repository at this point in the history
- use watchdog only in long-polling as SSE has auto-reconnection hand…
  • Loading branch information
ChristianMayer committed Oct 28, 2017
2 parents 8282125 + a33943a commit 9f22118
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
11 changes: 1 addition & 10 deletions client/source/class/cv/io/Client.js
Expand Up @@ -73,9 +73,6 @@ qx.Class.define('cv.io.Client', {

this.backendUrl = backendUrl;

this.watchdog = new cv.io.Watchdog();
this.watchdog.setClient(this);

this.addresses = [];
this.initialAddresses = [];
this.filters = [];
Expand Down Expand Up @@ -217,7 +214,6 @@ qx.Class.define('cv.io.Client', {
******************************************************
*/
members: {
watchdog: null,
backend: null,
backendName: null,
backendUrl: null,
Expand Down Expand Up @@ -340,8 +336,6 @@ qx.Class.define('cv.io.Client', {
if (this.loginSettings.loginOnly === true) {
// connect to the backend
this.getCurrentTransport().connect();
// start the watchdog
this.watchdog.start(5);
this.loginSettings.loginOnly = false;
}
else {
Expand Down Expand Up @@ -563,8 +557,6 @@ qx.Class.define('cv.io.Client', {
this.getCurrentTransport().handleSession(args, false);
} else {
this.getCurrentTransport().handleSession(args, true);
// once the connection is set up, start the watchdog
this.watchdog.start(5);
}
this.loginSettings.loggedIn = true;
if (this.loginSettings.callbackAfterLoggedIn) {
Expand All @@ -584,7 +576,6 @@ qx.Class.define('cv.io.Client', {
this.getCurrentTransport().abort();
}
this.loginSettings.loggedIn = false;
this.watchdog.stop();
},

/**
Expand Down Expand Up @@ -665,4 +656,4 @@ qx.Class.define('cv.io.Client', {
destruct: function() {
this.stop();
}
});
});
7 changes: 7 additions & 0 deletions client/source/class/cv/io/Watchdog.js
Expand Up @@ -67,6 +67,9 @@ qx.Class.define("cv.io.Watchdog", {
},

start: function (watchdogTimer) {
if (this.__id) {
this.stop();
}
this.__id = setInterval(this.aliveCheckFunction.bind(this), watchdogTimer * 1000);
},

Expand All @@ -77,6 +80,10 @@ qx.Class.define("cv.io.Watchdog", {
}
},

isActive: function() {
return !!this.__id;
},

ping: function (fullReload) {
this.last = new Date();
if (fullReload) {
Expand Down
18 changes: 13 additions & 5 deletions client/source/class/cv/io/transport/LongPolling.js
Expand Up @@ -32,6 +32,8 @@ qx.Class.define('cv.io.transport.LongPolling', {
*/
construct: function(client) {
this.client = client;
this.watchdog = new cv.io.Watchdog();
this.watchdog.setClient(client);
},


Expand All @@ -41,6 +43,7 @@ qx.Class.define('cv.io.transport.LongPolling', {
******************************************************
*/
members: {
watchdog: null,
doRestart: false, // are we currently in a restart, e.g. due to the watchdog
xhr: null, // the ongoing AJAX request
lastIndex: -1, // index returned by the last request
Expand Down Expand Up @@ -84,7 +87,7 @@ qx.Class.define('cv.io.transport.LongPolling', {
successCallback = this.handleRead;
}
this.__startReading(data, successCallback);
this.client.watchdog.start(5);
this.watchdog.start(5);
},

__startReading: function(data, callback) {
Expand Down Expand Up @@ -117,9 +120,13 @@ qx.Class.define('cv.io.transport.LongPolling', {
} else {
this.error("restarting XHR read requests in "+delay+" ms");
}
if (!this.watchdog.isActive()) {
// watchdog has been stopped in the abort function -> restart it
this.watchdog.start(5);
}
qx.event.Timer.once(function () {
this.__startReading();
this.client.watchdog.ping(true);
this.watchdog.ping(true);
}, this, delay);
}
return;
Expand All @@ -143,7 +150,7 @@ qx.Class.define('cv.io.transport.LongPolling', {
var url = this.xhr.getUrl().split("?").shift()+"?"+this.client.getQueryString(data);
this.xhr.setUrl(url);
this.xhr.send();
this.client.watchdog.ping();
this.watchdog.ping();
}
},

Expand All @@ -153,7 +160,7 @@ qx.Class.define('cv.io.transport.LongPolling', {
this.client.setDataReceived(false);
if (this.running) { // retry initial request
this.xhr.send();
this.client.watchdog.ping();
this.watchdog.ping();
}
return;
}
Expand All @@ -179,7 +186,7 @@ qx.Class.define('cv.io.transport.LongPolling', {
this.xhr.removeListener("success", this.handleReadStart, this);
this.xhr.addListener("success", this.handleRead, this);
this.xhr.send();
this.client.watchdog.ping();
this.watchdog.ping();
}
},

Expand Down Expand Up @@ -290,6 +297,7 @@ qx.Class.define('cv.io.transport.LongPolling', {
this.client.backend.hooks.onClose.bind(this);
}
}
this.watchdog.stop();
}
}
});
8 changes: 2 additions & 6 deletions client/source/class/cv/io/transport/Sse.js
Expand Up @@ -91,7 +91,6 @@ qx.Class.define('cv.io.transport.Sse', {
this.debug("connection established");
this.client.setConnected(true);
}.bind(this);
this.client.watchdog.start(5);
},

/**
Expand All @@ -101,7 +100,6 @@ qx.Class.define('cv.io.transport.Sse', {
this.client.record("read", e.data);
var json = JSON.parse(e.data);
var data = json.d;
this.client.watchdog.ping(true);
this.client.update(data);
this.client.setDataReceived(true);
},
Expand Down Expand Up @@ -160,12 +158,10 @@ qx.Class.define('cv.io.transport.Sse', {
},

/**
* Restart the read request, e.g. when the watchdog kicks in
*
*
* Restart the read request
*/
restart: function (doFullReload) {
if (doFullReload || !this.isConnectionRunning()) {
if (doFullReload || this.eventSource.readyState === EventSource.CLOSED) {
this.abort();
this.connect();
}
Expand Down

0 comments on commit 9f22118

Please sign in to comment.