From 1cc6cc6ae90a80edd28e95c1b73a9a25a57a25b2 Mon Sep 17 00:00:00 2001 From: Oliver Giles Date: Fri, 19 Jun 2020 12:01:46 +1200 Subject: [PATCH] unify SSE reconnect behaviour Chrome auto-reconnects when an EventSource connection is interrupted but Firefox doesn't. Enforce consistent behaviour by implementing reconnect logic. --- src/resources/js/app.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/resources/js/app.js b/src/resources/js/app.js index 4d8fcb9..0d479e4 100644 --- a/src/resources/js/app.js +++ b/src/resources/js/app.js @@ -23,8 +23,9 @@ const timeScale = function(max){ : { scale:function(v){return v;}, label:'Seconds' }; } const ServerEventHandler = function() { - function setupEventSource(path, query, next) { + function setupEventSource(path, query, next, comp) { const es = new EventSource(document.head.baseURI + path.substr(1) + query); + es.comp = comp; es.path = path; // save for later in case we need to add query params es.onmessage = function(msg) { msg = JSON.parse(msg.data); @@ -46,6 +47,7 @@ const ServerEventHandler = function() { this.comp = comp; // 2. needed to close the ws on navigation away comp.es = this; + comp.esReconnectInterval = 500; // Update html and nav titles document.title = comp.$root.title = msg.title; // Calculate clock offset (used by ProgressUpdater) @@ -66,8 +68,14 @@ const ServerEventHandler = function() { } } } - es.onerror = function() { + es.onerror = function(e) { this.comp.$root.connected = false; + setTimeout(() => { + this.comp.es = setupEventSource(path, query, null, this.comp); + }, this.comp.esReconnectInterval); + if(this.comp.esReconnectInterval < 7500) + this.comp.esReconnectInterval *= 1.5; + this.close(); } return es; }