From d6a302ecee9d208f4dc04ca936569a2fe80640ce Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Tue, 15 Feb 2022 14:25:26 +0200 Subject: [PATCH] minecraft: reconnect to bridge gateway on error as well --- scripts/minecraft.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/scripts/minecraft.js b/scripts/minecraft.js index b8f3f84..a448c49 100644 --- a/scripts/minecraft.js +++ b/scripts/minecraft.js @@ -40,6 +40,17 @@ export default class GatewayClient { if (!token) token = this.token; + if (this.ws) { + console.log("gateway: connect() but connection already exists, killing existing connection..."); + try { + this.ws.removeAllListeners(); + this.ws.close(); + this.ws = null; + } catch (e) { + console.log("gateway: error while closing existing connection - it might not be established yet"); + } + } + console.log("gateway: connecting"); this.ws = new WebSocket(this.gatewayPath); @@ -99,6 +110,14 @@ export default class GatewayClient { this.connect(token); }, 4000); }); + this.ws.on("error", (e) => { + console.error("gateway: error", e); + console.log("gateway: reconnecting in 4000ms due to previous error"); + setTimeout(() => { + console.log("gateway: reconnecting"); + this.connect(token); + }, 4000); + }); } _checkMessageSchema(message) {