From eb898033984737c88b02d7a2ed9f622031ed1b72 Mon Sep 17 00:00:00 2001 From: hippoz Date: Fri, 20 Aug 2021 20:00:55 +0300 Subject: [PATCH] add basic and somewhat clunky automatic reconnection support for the gateway --- .../src/API/Gateway/GatewayConnection.js | 3 +++ .../API/Gateway/globalGatewayConnection.js | 23 +++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/bfrontend/src/API/Gateway/GatewayConnection.js b/bfrontend/src/API/Gateway/GatewayConnection.js index 684db8f..1fc225c 100644 --- a/bfrontend/src/API/Gateway/GatewayConnection.js +++ b/bfrontend/src/API/Gateway/GatewayConnection.js @@ -61,6 +61,9 @@ class GatewayConnection { } GatewayConnection.prototype.connect = function(token) { + if (token) this.token = token; + if (this.token) token = this.token; + this.ws = new WebSocket(this.gatewayUrl); this.handshakeCompleted = false; diff --git a/bfrontend/src/API/Gateway/globalGatewayConnection.js b/bfrontend/src/API/Gateway/globalGatewayConnection.js index c559694..346fa38 100644 --- a/bfrontend/src/API/Gateway/globalGatewayConnection.js +++ b/bfrontend/src/API/Gateway/globalGatewayConnection.js @@ -3,17 +3,23 @@ import config from '../../Config'; import store from '../../Global/store'; import logger from '../../Util/Logger'; -const { warn } = logger(["Experiments"]); +const { warn, log } = logger(["globalGatewayConnection"]); +const { warn: experimentsWarn } = logger(["globalGatewayConnection", "Experiments"]); const globalGatewayConnection = new GatewayConnection(config.gatewayUrl); +let currentlyReconnecting = false; globalGatewayConnection.onopen = (sessionData) => { + if (currentlyReconnecting) { + store.dispatch({ type: 'application/updatebannertext', text: undefined }); + currentlyReconnecting = false; + } store.dispatch({ type: 'gateway/connectionstatus', gateway: { isConnected: true } }); store.dispatch({ type: 'authenticator/updatelocaluserobject', user: sessionData.user }); store.dispatch({ type: 'channels/updatechannellist', channels: sessionData.channels }); if (localStorage.getItem("enableExperimentOverrides")) { - warn("Experiment overrides are enabled"); + experimentsWarn("Experiment overrides are enabled"); const experimentModifiers = JSON.parse(localStorage.getItem("experimentOverrides")); const experiments = { ...sessionData.__global_experiments || {}, @@ -35,8 +41,21 @@ globalGatewayConnection.presenceUpdate = (presence) => { }; globalGatewayConnection.onclose = function() { + if (currentlyReconnecting || globalGatewayConnection.ws.readyState === 1) return; + currentlyReconnecting = true; + warn("Gateway connection closed"); store.dispatch({ type: 'authenticator/updatelocaluserobject', user: undefined }); store.dispatch({ type: 'gateway/connectionstatus', gateway: { isConnected: false } }); + store.dispatch({ type: 'application/updatebannertext', text: "⚡ Hang tight! You've lost connection!" }); + const interval = setInterval(() => { + if (globalGatewayConnection.ws.readyState === 1) { + clearInterval(interval); + currentlyReconnecting = false; + return; + } + log("Attempting reconnection..."); + globalGatewayConnection.connect(); + }, 5000); }; export default globalGatewayConnection;