add support for gateway pings

This commit is contained in:
hippoz 2021-09-08 00:18:59 +03:00
parent 76e4fc8fee
commit 6b3751aff2
No known key found for this signature in database
GPG key ID: 7C52899193467641
2 changed files with 15 additions and 18 deletions

View file

@ -11,6 +11,7 @@ const opcodes = {
4: { name: "EVENT_CREATE_MESSAGE", data: "JSON" }, 4: { name: "EVENT_CREATE_MESSAGE", data: "JSON" },
5: { name: "ACTION_UPDATE_STATUS", data: "JSON" }, 5: { name: "ACTION_UPDATE_STATUS", data: "JSON" },
6: { name: "EVENT_CHANNEL_MEMBERS", data: "JSON" }, 6: { name: "EVENT_CHANNEL_MEMBERS", data: "JSON" },
7: { name: "ACTION_PING", data: "string" },
21: { name: "ACTION_VOICE_REQUEST_SESSION", data: "JSON" }, 21: { name: "ACTION_VOICE_REQUEST_SESSION", data: "JSON" },
22: { name: "EVENT_VOICE_ASSIGN_SERVER", data: "JSON" }, 22: { name: "EVENT_VOICE_ASSIGN_SERVER", data: "JSON" },
23: { name: "ACTION_VOICE_CONNECTION_REQUEST", 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.onopen = () => logGateway("Open");
this.ws.onclose = (e) => { this.ws.onclose = (e) => {
this.handshakeCompleted = false; this.handshakeCompleted = false;
if (this.pingIntervalFunction) clearInterval(this.pingIntervalFunction);
logGateway(`Close: ${e.code}:${e.reason}`); logGateway(`Close: ${e.code}:${e.reason}`);
this.fire("onclose", e.code); this.fire("onclose", e.code);
}; };
@ -86,6 +88,7 @@ GatewayConnection.prototype.connect = function(token) {
case "HELLO": { case "HELLO": {
// Got HELLO from server, send YOO as soon as possible // Got HELLO from server, send YOO as soon as possible
logGateway("Got HELLO", packet.data); logGateway("Got HELLO", packet.data);
this.helloData = packet.data;
logGateway("Sending YOO"); logGateway("Sending YOO");
this.ws.send(this.packet("YOO", { token })); this.ws.send(this.packet("YOO", { token }));
break; break;
@ -109,7 +112,9 @@ GatewayConnection.prototype.connect = function(token) {
logGateway("Got YOO_ACK", packet.data); logGateway("Got YOO_ACK", packet.data);
this.handshakeCompleted = true; this.handshakeCompleted = true;
this.sessionInformation = packet.data; this.sessionInformation = packet.data;
this.pingIntervalFunction = setInterval(this.sendPing.bind(this), this.helloData.pingInterval);
this.handshakeCompletedTime = (new Date()).getTime() - this.connectionAttemptStartedTime.getTime(); this.handshakeCompletedTime = (new Date()).getTime() - this.connectionAttemptStartedTime.getTime();
logGateway(`Set PING interval to ${this.helloData.pingInterval}ms`);
logGateway(`Handshake complete in ${this.handshakeCompletedTime}ms`); logGateway(`Handshake complete in ${this.handshakeCompletedTime}ms`);
this.fire("onopen", packet.data); this.fire("onopen", packet.data);
break; 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) { 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", { this.ws.send(this.packet("ACTION_CREATE_MESSAGE", {
content, content,

View file

@ -3,7 +3,7 @@ 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, log } = logger(["globalGatewayConnection"]); const { log } = logger(["globalGatewayConnection"]);
const { warn: experimentsWarn } = logger(["globalGatewayConnection", "Experiments"]); const { warn: experimentsWarn } = logger(["globalGatewayConnection", "Experiments"]);
const globalGatewayConnection = new GatewayConnection(config.gatewayUrl); const globalGatewayConnection = new GatewayConnection(config.gatewayUrl);
@ -45,24 +45,12 @@ const dispatchConnectionClose = () => {
store.dispatch({ type: 'application/updatebannertext', text: "Hang tight! You've lost connection!" }); store.dispatch({ type: 'application/updatebannertext', text: "Hang tight! You've lost connection!" });
}; };
let reconnectInterval;
globalGatewayConnection.onclose = function(code) { globalGatewayConnection.onclose = function(code) {
if (code === 4006) {
clearInterval(reconnectInterval);
dispatchConnectionClose();
currentlyReconnecting = true; // NOTE: no currentlyReconnecting = true; // NOTE: no
return;
}
if (currentlyReconnecting || globalGatewayConnection.ws.readyState === 1) return;
currentlyReconnecting = true;
warn("Gateway connection closed");
dispatchConnectionClose(); dispatchConnectionClose();
reconnectInterval = setInterval(() => { if (code === 4004) return;
if (globalGatewayConnection.ws.readyState === 1) { setTimeout(() => {
clearInterval(reconnectInterval); if (globalGatewayConnection.ws.readyState === 0 || globalGatewayConnection.ws.readyState === 1) return;
currentlyReconnecting = false;
return;
}
log("Attempting reconnection..."); log("Attempting reconnection...");
globalGatewayConnection.connect(); globalGatewayConnection.connect();
}, 5000); }, 5000);