diff --git a/bfrontend/src/API/Gateway/GatewayConnection.js b/bfrontend/src/API/Gateway/GatewayConnection.js index 49701ae..c489059 100644 --- a/bfrontend/src/API/Gateway/GatewayConnection.js +++ b/bfrontend/src/API/Gateway/GatewayConnection.js @@ -11,6 +11,7 @@ const opcodes = { 4: { name: "EVENT_CREATE_MESSAGE", data: "JSON" }, 5: { name: "ACTION_UPDATE_STATUS", data: "JSON" }, 6: { name: "EVENT_CHANNEL_MEMBERS", data: "JSON" }, + 7: { name: "ACTION_PING", data: "string" }, 21: { name: "ACTION_VOICE_REQUEST_SESSION", data: "JSON" }, 22: { name: "EVENT_VOICE_ASSIGN_SERVER", data: "JSON" }, 23: { name: "ACTION_VOICE_CONNECTION_REQUEST", data: "JSON" }, @@ -74,6 +75,7 @@ GatewayConnection.prototype.connect = function(token) { this.ws.onopen = () => logGateway("Open"); this.ws.onclose = (e) => { this.handshakeCompleted = false; + if (this.pingIntervalFunction) clearInterval(this.pingIntervalFunction); logGateway(`Close: ${e.code}:${e.reason}`); this.fire("onclose", e.code); }; @@ -86,6 +88,7 @@ GatewayConnection.prototype.connect = function(token) { case "HELLO": { // Got HELLO from server, send YOO as soon as possible logGateway("Got HELLO", packet.data); + this.helloData = packet.data; logGateway("Sending YOO"); this.ws.send(this.packet("YOO", { token })); break; @@ -109,7 +112,9 @@ GatewayConnection.prototype.connect = function(token) { logGateway("Got YOO_ACK", packet.data); this.handshakeCompleted = true; this.sessionInformation = packet.data; + this.pingIntervalFunction = setInterval(this.sendPing.bind(this), this.helloData.pingInterval); this.handshakeCompletedTime = (new Date()).getTime() - this.connectionAttemptStartedTime.getTime(); + logGateway(`Set PING interval to ${this.helloData.pingInterval}ms`); logGateway(`Handshake complete in ${this.handshakeCompletedTime}ms`); this.fire("onopen", packet.data); break; @@ -160,8 +165,12 @@ GatewayConnection.prototype.connect = function(token) { }; }; +GatewayConnection.prototype.sendPing = function() { + this.ws.send(this.packet("ACTION_PING", 1)); +}; + GatewayConnection.prototype.sendMessage = function(content, channelId) { - if (!this.sessionInformation) throw new Error("gateway: tried to send message before handshake completion"); + if (!this.sessionInformation) return; this.ws.send(this.packet("ACTION_CREATE_MESSAGE", { content, diff --git a/bfrontend/src/API/Gateway/globalGatewayConnection.js b/bfrontend/src/API/Gateway/globalGatewayConnection.js index 9ab0aaa..859c085 100644 --- a/bfrontend/src/API/Gateway/globalGatewayConnection.js +++ b/bfrontend/src/API/Gateway/globalGatewayConnection.js @@ -3,7 +3,7 @@ import config from '../../Config'; import store from '../../Global/store'; import logger from '../../Util/Logger'; -const { warn, log } = logger(["globalGatewayConnection"]); +const { log } = logger(["globalGatewayConnection"]); const { warn: experimentsWarn } = logger(["globalGatewayConnection", "Experiments"]); const globalGatewayConnection = new GatewayConnection(config.gatewayUrl); @@ -45,24 +45,12 @@ const dispatchConnectionClose = () => { store.dispatch({ type: 'application/updatebannertext', text: "Hang tight! You've lost connection!" }); }; -let reconnectInterval; globalGatewayConnection.onclose = function(code) { - if (code === 4006) { - clearInterval(reconnectInterval); - dispatchConnectionClose(); - currentlyReconnecting = true; // NOTE: no - return; - } - if (currentlyReconnecting || globalGatewayConnection.ws.readyState === 1) return; - currentlyReconnecting = true; - warn("Gateway connection closed"); + currentlyReconnecting = true; // NOTE: no dispatchConnectionClose(); - reconnectInterval = setInterval(() => { - if (globalGatewayConnection.ws.readyState === 1) { - clearInterval(reconnectInterval); - currentlyReconnecting = false; - return; - } + if (code === 4004) return; + setTimeout(() => { + if (globalGatewayConnection.ws.readyState === 0 || globalGatewayConnection.ws.readyState === 1) return; log("Attempting reconnection..."); globalGatewayConnection.connect(); }, 5000);