From 91bdffab3376d8bfb490d10a2d8913d77ede96ad Mon Sep 17 00:00:00 2001 From: Mark Walker Date: Sun, 10 May 2015 18:58:18 -0700 Subject: [PATCH 1/2] Stop sockjs from multiplying on reconnect Have you ever noticed when developing that every time you stop and start the server, the terminal window gets an extra duplicate line for every reconnect attempt? Well, it's because (I think) "delete" in javascript just removes the indicated name from the namespace, it doesn't actually free up an object. Those zombie objects are still there and wake up (for some transports) on reconnect. Might be different in SockJS v1 or later. --- src/octoprint/static/js/app/dataupdater.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/octoprint/static/js/app/dataupdater.js b/src/octoprint/static/js/app/dataupdater.js index d547a16c..4172b459 100644 --- a/src/octoprint/static/js/app/dataupdater.js +++ b/src/octoprint/static/js/app/dataupdater.js @@ -22,6 +22,7 @@ function DataUpdater(allViewModels) { }; self.reconnect = function() { + self._socket.close(); delete self._socket; self.connect(); }; @@ -31,7 +32,11 @@ function DataUpdater(allViewModels) { self._autoReconnectTrial = 0; }; - self._onclose = function() { + self._onclose = function(e) { + if (e.code == 1000) { + // it was us calling close + return; + } if (self._autoReconnectTrial >= self._autoReconnectDialogIndex) { // Only consider it a real disconnect if the trial number has exceeded our threshold. From c8b8bcb22ef58da7afde8da9de31d03ca73bb5ee Mon Sep 17 00:00:00 2001 From: Mark Walker Date: Mon, 11 May 2015 05:52:36 -0700 Subject: [PATCH 2/2] Magic CLOSE_NORMAL See https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent WebSocket defines 1000 as CLOSE_NORMAL, SockJS uses it without a name and doesn't provide one for us, so for now we define our own --- src/octoprint/static/js/app/dataupdater.js | 3 +-- src/octoprint/templates/initscript.jinja2 | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/octoprint/static/js/app/dataupdater.js b/src/octoprint/static/js/app/dataupdater.js index 4172b459..73a4a6d2 100644 --- a/src/octoprint/static/js/app/dataupdater.js +++ b/src/octoprint/static/js/app/dataupdater.js @@ -33,8 +33,7 @@ function DataUpdater(allViewModels) { }; self._onclose = function(e) { - if (e.code == 1000) { - // it was us calling close + if (e.code == SOCKJS_CLOSE_NORMAL) { return; } if (self._autoReconnectTrial >= self._autoReconnectDialogIndex) { diff --git a/src/octoprint/templates/initscript.jinja2 b/src/octoprint/templates/initscript.jinja2 index 2351db39..4d8094a8 100644 --- a/src/octoprint/templates/initscript.jinja2 +++ b/src/octoprint/templates/initscript.jinja2 @@ -20,6 +20,8 @@ var SOCKJS_URI = "{{ url_for('index') }}" + "sockjs"; var SOCKJS_DEBUG = CONFIG_DEBUG; + // sockjs should define CLOSE_NORMAL for us, but they don't (from ws spec) + var SOCKJS_CLOSE_NORMAL = 1000 var UI_API_KEY = "{{ uiApiKey }}"; var VERSION = "{{ version }}";