From 8be4f3024ade97ea2ecb7366b3fda5d297e34723 Mon Sep 17 00:00:00 2001 From: Aurélien Bompard Date: Jul 21 2017 15:46:24 +0000 Subject: [PATCH 1/3] Automatically reconnect the EventSource --- diff --git a/hubs/static/client/app/core/Hubs.js b/hubs/static/client/app/core/Hubs.js index fa53ab5..6f24426 100644 --- a/hubs/static/client/app/core/Hubs.js +++ b/hubs/static/client/app/core/Hubs.js @@ -1,4 +1,5 @@ import HubConfig from './HubConfig.jsx'; import Streams from './Streams.jsx'; +import ReconnectingEventSource from "reconnecting-eventsource"; -module.exports = {HubConfig, Streams} +module.exports = {HubConfig, Streams, ReconnectingEventSource} diff --git a/hubs/static/client/package.json b/hubs/static/client/package.json index c37dc96..d0e6a6c 100644 --- a/hubs/static/client/package.json +++ b/hubs/static/client/package.json @@ -18,6 +18,7 @@ "react-intl": "~2.1.5", "react-linkify": "^0.1.3", "react-timeago": "^3.1.1", + "reconnecting-eventsource": "^1.0.1", "webpack": "~1.13.1" }, "devDependencies": { diff --git a/hubs/static/js/utils.js b/hubs/static/js/utils.js index 2169c6b..cefe086 100644 --- a/hubs/static/js/utils.js +++ b/hubs/static/js/utils.js @@ -38,7 +38,7 @@ function setup_sse(url) { // Auto-update is disabled. return; } - var sseSource = new EventSource(url); + var sseSource = new Hubs.ReconnectingEventSource(url); sseSource.addEventListener("hubs:widget-updated", function(e) { var widget = $("#widget-" + e.data); if (widget.length === 0) { return; } From 924495183297e6936ea29589da60bf98a3d344e1 Mon Sep 17 00:00:00 2001 From: Aurélien Bompard Date: Jul 21 2017 15:49:54 +0000 Subject: [PATCH 2/3] Refactor the notification icon --- diff --git a/hubs/static/js/utils.js b/hubs/static/js/utils.js index cefe086..63f3ed0 100644 --- a/hubs/static/js/utils.js +++ b/hubs/static/js/utils.js @@ -38,7 +38,8 @@ function setup_sse(url) { // Auto-update is disabled. return; } - var sseSource = new Hubs.ReconnectingEventSource(url); + var sseSource = new Hubs.ReconnectingEventSource(url), + notifTimeout = null; sseSource.addEventListener("hubs:widget-updated", function(e) { var widget = $("#widget-" + e.data); if (widget.length === 0) { return; } @@ -57,16 +58,20 @@ function setup_sse(url) { }, }); }); - // On each message, switch the notification favicon to green for 15s. - sseSource.addEventListener("message", function(e) { - Notificon('#33ff00'); // green - setTimeout(function(){ Notificon(); }, 15000) - }); - // On error, switch the notification favicon to red. - sseSource.addEventListener("error", function(e) { - Notificon('#eb361e'); // red - setTimeout(function(){ Notificon(); }, 15000) - }); + function change_notify_icon(color) { + // Set the notify icon for 5s + if (color == "green") { + color = "#33ff00"; + } else if (color == "red") { + color = "#eb361e"; + } + Notificon(color); + if (notifTimeout) { clearTimeout(notifTimeout); } + notifTimeout = setTimeout(function(){ Notificon(); }, 5000) + } + // Icon to red on disconnect, icon to green on reconnect. + sseSource.addEventListener("error", function() { change_notify_icon("red"); }); + sseSource.addEventListener("open", function() { change_notify_icon("green"); }); // Make it accessible by other components on the page. document.sseSource = sseSource; } From f7d8a3cd4bca27036f862d0fd9930d72b2a2d304 Mon Sep 17 00:00:00 2001 From: Aurélien Bompard Date: Jul 21 2017 16:42:28 +0000 Subject: [PATCH 3/3] Remove useless debug output --- diff --git a/hubs/static/client/app/components/feed/ItemsGetter.jsx b/hubs/static/client/app/components/feed/ItemsGetter.jsx index ca6397d..678f420 100644 --- a/hubs/static/client/app/components/feed/ItemsGetter.jsx +++ b/hubs/static/client/app/components/feed/ItemsGetter.jsx @@ -72,7 +72,6 @@ export default class ItemsGetter extends React.Component { } handleSSEEventError(e) { - console.log(e); if (this.sseSource.readyState === 2) { // Connection closed. this.setState({