add basic and somewhat clunky automatic reconnection support for the gateway

This commit is contained in:
hippoz 2021-08-20 20:00:55 +03:00
parent 551d83fc06
commit eb89803398
Signed by: hippoz
GPG key ID: 7C52899193467641
2 changed files with 24 additions and 2 deletions

View file

@ -61,6 +61,9 @@ class GatewayConnection {
} }
GatewayConnection.prototype.connect = function(token) { GatewayConnection.prototype.connect = function(token) {
if (token) this.token = token;
if (this.token) token = this.token;
this.ws = new WebSocket(this.gatewayUrl); this.ws = new WebSocket(this.gatewayUrl);
this.handshakeCompleted = false; this.handshakeCompleted = false;

View file

@ -3,17 +3,23 @@ import config from '../../Config';
import store from '../../Global/store'; import store from '../../Global/store';
import logger from '../../Util/Logger'; 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); const globalGatewayConnection = new GatewayConnection(config.gatewayUrl);
let currentlyReconnecting = false;
globalGatewayConnection.onopen = (sessionData) => { 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: 'gateway/connectionstatus', gateway: { isConnected: true } });
store.dispatch({ type: 'authenticator/updatelocaluserobject', user: sessionData.user }); store.dispatch({ type: 'authenticator/updatelocaluserobject', user: sessionData.user });
store.dispatch({ type: 'channels/updatechannellist', channels: sessionData.channels }); store.dispatch({ type: 'channels/updatechannellist', channels: sessionData.channels });
if (localStorage.getItem("enableExperimentOverrides")) { if (localStorage.getItem("enableExperimentOverrides")) {
warn("Experiment overrides are enabled"); experimentsWarn("Experiment overrides are enabled");
const experimentModifiers = JSON.parse(localStorage.getItem("experimentOverrides")); const experimentModifiers = JSON.parse(localStorage.getItem("experimentOverrides"));
const experiments = { const experiments = {
...sessionData.__global_experiments || {}, ...sessionData.__global_experiments || {},
@ -35,8 +41,21 @@ globalGatewayConnection.presenceUpdate = (presence) => {
}; };
globalGatewayConnection.onclose = function() { 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: 'authenticator/updatelocaluserobject', user: undefined });
store.dispatch({ type: 'gateway/connectionstatus', gateway: { isConnected: false } }); 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; export default globalGatewayConnection;