From 80879676832479912d2ece12960257ff7bb67e83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Fri, 24 Jan 2020 16:54:52 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20behaviour=20when=20page=20?= =?UTF-8?q?cached=20&=20server=20down?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit E.g. in Firefox reopen tab while server is down - FF will use a cached copy and for some reason sockjs won't notice that stuff is down. So now we have a timeout of 5s for initial connection that takes care of that. --- src/octoprint/static/js/app/client/socket.js | 19 +++++++++++++++++++ src/octoprint/static/js/app/dataupdater.js | 9 +++++++-- src/octoprint/static/js/app/main.js | 4 ++++ .../static/js/app/viewmodels/uistate.js | 2 +- src/octoprint/templates/index.jinja2 | 2 +- 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/octoprint/static/js/app/client/socket.js b/src/octoprint/static/js/app/client/socket.js index 9562ed017..188886f41 100644 --- a/src/octoprint/static/js/app/client/socket.js +++ b/src/octoprint/static/js/app/client/socket.js @@ -13,6 +13,7 @@ this.options = { timeouts: [0, 1, 1, 2, 3, 5, 8, 13, 20, 40, 100], + connectTimeout: 5000, rateSlidingWindowSize: 20 }; @@ -24,6 +25,16 @@ this.rateThrottleFactor = 1; this.rateBase = 500; this.rateLastMeasurements = []; + + this.connectTimeout = undefined; + + this.onMessage("connected", function() { + // Make sure to clear connection timeout on connect + if (self.connectTimeout) { + clearTimeout(self.connectTimeout); + self.connectTimeout = undefined; + } + }) }; OctoPrintSocketClient.prototype.propagateMessage = function(event, data) { @@ -139,6 +150,13 @@ }); }; + if (self.connectTimeout) { + clearTimeout(self.connectTimeout); + } + this.connectTimeout = setTimeout(function() { + self.onConnectTimeout(); + }, self.options.connectTimeout); + this.socket = new SockJS(url + "sockjs", undefined, opts); this.socket.onopen = onOpen; this.socket.onclose = onClose; @@ -169,6 +187,7 @@ OctoPrintSocketClient.prototype.onReconnectFailed = function() {}; OctoPrintSocketClient.prototype.onConnected = function() {}; OctoPrintSocketClient.prototype.onDisconnected = function(code) {}; + OctoPrintSocketClient.prototype.onConnectTimeout = function() {}; OctoPrintSocketClient.prototype.onRateTooLow = function(measured, minimum) { this.increaseRate(); diff --git a/src/octoprint/static/js/app/dataupdater.js b/src/octoprint/static/js/app/dataupdater.js index bd1de144d..ebb276a4a 100644 --- a/src/octoprint/static/js/app/dataupdater.js +++ b/src/octoprint/static/js/app/dataupdater.js @@ -45,7 +45,7 @@ function DataUpdater(allViewModels, connectCallback, disconnectCallback) { self.connect = function() { if (self._connectedDeferred) { - self._connectedDeferred.reject(); + self._connectedDeferred.reject("reconnect"); } self._connectedDeferred = $.Deferred(); OctoPrint.socket.connect({debug: !!SOCKJS_DEBUG}); @@ -54,7 +54,7 @@ function DataUpdater(allViewModels, connectCallback, disconnectCallback) { self.reconnect = function() { if (self._connectedDeferred) { - self._connectedDeferred.reject(); + self._connectedDeferred.reject("reconnect"); } self._connectedDeferred = $.Deferred(); OctoPrint.socket.reconnect(); @@ -121,6 +121,10 @@ function DataUpdater(allViewModels, connectCallback, disconnectCallback) { } }; + self._onConnectTimeout = function() { + self._connectedDeferred.reject("timeout"); + }; + self._onConnectMessage = function(event) { if (self._initializedDeferred) { self._initializedDeferred.reject(); @@ -399,6 +403,7 @@ function DataUpdater(allViewModels, connectCallback, disconnectCallback) { OctoPrint.socket.onDisconnected = self._onDisconnected; OctoPrint.socket.onReconnectAttempt = self._onReconnectAttempt; OctoPrint.socket.onReconnectFailed = self._onReconnectFailed; + OctoPrint.socket.onConnectTimeout = self._onConnectTimeout; OctoPrint.socket.onRateTooHigh = self._onDecreaseRate; OctoPrint.socket.onRateTooLow = self._onIncreaseRate; OctoPrint.socket diff --git a/src/octoprint/static/js/app/main.js b/src/octoprint/static/js/app/main.js index 605d23e58..d50c67d18 100644 --- a/src/octoprint/static/js/app/main.js +++ b/src/octoprint/static/js/app/main.js @@ -808,6 +808,10 @@ $(function() { // make sure we trigger onServerConnect should we dis- and reconnect to the server dataUpdater.connectCallback = onServerConnect; + // we are now connected to the server and need to change the loading message - jquery instead of + // binding because no bindings yet + $("#page-container-loading-header").html(gettext("Loading OctoPrint's UI, please wait...")); + // perform passive login first onServerConnect() .done(function() { diff --git a/src/octoprint/static/js/app/viewmodels/uistate.js b/src/octoprint/static/js/app/viewmodels/uistate.js index 35fd6c2d5..89390ad76 100644 --- a/src/octoprint/static/js/app/viewmodels/uistate.js +++ b/src/octoprint/static/js/app/viewmodels/uistate.js @@ -52,7 +52,7 @@ $(function() { // we can't do this with bindings since the bindings are not initialized yet if we need this $("#page-container-loading-header").text("Loading failed"); $("#page-container-loading-spinner").removeClass("fa-spinner fa-spin").addClass("fa-exclamation-triangle text-error"); - $("#page-container-loading-error").html(error + " " + _.sprintf("Please check your browser's error console and octoprint.log for possible reasons.", {browser: "https://faq.octoprint.org/browser-console", octoprint: "https://faq.octoprint.org/logs"})).show(); + $("#page-container-loading-error").html(error + " " + _.sprintf("Please check your browser's error console and octoprint.log for possible reasons.Also make sure that the server is actually running by reloading this page.", {browser: "https://faq.octoprint.org/browser-console", octoprint: "https://faq.octoprint.org/logs"})).show(); }; } diff --git a/src/octoprint/templates/index.jinja2 b/src/octoprint/templates/index.jinja2 index c9333410c..41e216b70 100644 --- a/src/octoprint/templates/index.jinja2 +++ b/src/octoprint/templates/index.jinja2 @@ -44,7 +44,7 @@
-

{{ _('Loading OctoPrint\'s UI, please wait...') }}

+

{{ _('Connecting to OctoPrint\'s server...') }}