From 487f3ea4f43c62ff29ee3e01d36c5b8b5c0c317c Mon Sep 17 00:00:00 2001 From: abalabahaha Date: Fri, 15 Apr 2016 23:08:55 -0700 Subject: [PATCH] Sketchy autoreconnect --- lib/Client/InternalClient.js | 31 ++++++++++++++++++++----------- src/Client/InternalClient.js | 31 ++++++++++++++++++++----------- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/lib/Client/InternalClient.js b/lib/Client/InternalClient.js index b698f06a4..ec26473d6 100644 --- a/lib/Client/InternalClient.js +++ b/lib/Client/InternalClient.js @@ -199,6 +199,7 @@ var InternalClient = (function () { this.servers = new _UtilCache2["default"](); this.unavailableServers = new _UtilCache2["default"](); this.private_channels = new _UtilCache2["default"](); + this.autoReconnectInterval = 1000; this.intervals = { typing: [], @@ -240,7 +241,7 @@ var InternalClient = (function () { InternalClient.prototype.disconnected = function disconnected() { var _this2 = this; - var forced = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0]; + var autoReconnect = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0]; this.cleanIntervals(); @@ -248,15 +249,18 @@ var InternalClient = (function () { _this2.leaveVoiceChannel(vc); }); - if (this.client.options.revive && !forced) { - this.setup(); + if (autoReconnect) { + this.autoReconnectInterval = Math.min(this.autoReconnectInterval * (Math.random() + 1), 60000); + setTimeout(function () { + _this2.setup(); - // Check whether the email is set (if not, only a token has been used for login) - if (this.email) { - this.login(this.email, this.password); - } else { - this.loginWithToken(this.token); - } + // Check whether the email is set (if not, only a token has been used for login) + if (_this2.email) { + _this2.login(_this2.email, _this2.password); + } else { + _this2.loginWithToken(_this2.token); + } + }, this.autoReconnectInterval); } this.client.emit("disconnected"); @@ -669,6 +673,7 @@ var InternalClient = (function () { _this13.websocket = null; throw error; })["catch"](function (error) { + _this13.websocket = null; _this13.state = _ConnectionState2["default"].DISCONNECTED; client.emit("disconnected"); throw error; @@ -1623,14 +1628,17 @@ var InternalClient = (function () { }); }; - this.websocket.onclose = function () { + this.websocket.onclose = function (code) { self.websocket = null; self.state = _ConnectionState2["default"].DISCONNECTED; - self.disconnected(); + self.disconnected(_this40.client.options.autoReconnect); }; this.websocket.onerror = function (e) { client.emit("error", e); + self.websocket = null; + self.state = _ConnectionState2["default"].DISCONNECTED; + self.disconnected(_this40.client.options.autoReconnect); }; this.websocket.onmessage = function (e) { @@ -1662,6 +1670,7 @@ var InternalClient = (function () { _this40.forceFetchCount = {}; _this40.forceFetchQueue = []; _this40.forceFetchLength = 1; + _this40.autoReconnectInterval = 1000; data.guilds.forEach(function (server) { if (!server.unavailable) { diff --git a/src/Client/InternalClient.js b/src/Client/InternalClient.js index c7950f941..ea66a3862 100644 --- a/src/Client/InternalClient.js +++ b/src/Client/InternalClient.js @@ -136,6 +136,7 @@ export default class InternalClient { this.servers = new Cache(); this.unavailableServers = new Cache(); this.private_channels = new Cache(); + this.autoReconnectInterval = 1000; this.intervals = { typing : [], @@ -161,7 +162,7 @@ export default class InternalClient { } } - disconnected(forced = false) { + disconnected(autoReconnect = false) { this.cleanIntervals(); @@ -169,15 +170,18 @@ export default class InternalClient { this.leaveVoiceChannel(vc); }); - if (this.client.options.revive && !forced) { - this.setup(); + if (autoReconnect) { + this.autoReconnectInterval = Math.min(this.autoReconnectInterval * (Math.random() + 1), 60000); + setTimeout(() => { + this.setup(); - // Check whether the email is set (if not, only a token has been used for login) - if (this.email) { - this.login(this.email, this.password); - } else { - this.loginWithToken(this.token); - } + // Check whether the email is set (if not, only a token has been used for login) + if (this.email) { + this.login(this.email, this.password); + } else { + this.loginWithToken(this.token); + } + }, this.autoReconnectInterval); } this.client.emit("disconnected"); @@ -544,6 +548,7 @@ export default class InternalClient { throw error; }) .catch(error => { + this.websocket = null; this.state = ConnectionState.DISCONNECTED; client.emit("disconnected"); throw error; @@ -1381,14 +1386,17 @@ export default class InternalClient { }); }; - this.websocket.onclose = () => { + this.websocket.onclose = (code) => { self.websocket = null; self.state = ConnectionState.DISCONNECTED; - self.disconnected(); + self.disconnected(this.client.options.autoReconnect); }; this.websocket.onerror = e => { client.emit("error", e); + self.websocket = null; + self.state = ConnectionState.DISCONNECTED; + self.disconnected(this.client.options.autoReconnect); }; this.websocket.onmessage = e => { @@ -1418,6 +1426,7 @@ export default class InternalClient { this.forceFetchCount = {}; this.forceFetchQueue = []; this.forceFetchLength = 1; + this.autoReconnectInterval = 1000; data.guilds.forEach(server => { if (!server.unavailable) {