From a757e078da836700fd3b90c7d92de7311020a8b0 Mon Sep 17 00:00:00 2001 From: Teemu Kupari Date: Wed, 3 Aug 2016 12:14:29 +0300 Subject: [PATCH] store timeout to prevent duplicate reconnections --- lib/connection.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index c58c90b..a84a83a 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -12,6 +12,7 @@ module.exports = function (ccg) { ccg.prototype.response = ""; ccg.prototype.readyForNextCommand = true; ccg.prototype.waitingForDoubleTerminator = false; + ccg.prototype.reconnectTimeout = null; // ccg.connect // ----------- @@ -67,8 +68,9 @@ module.exports = function (ccg) { self.emit("connectionError", err); - if (!self.disconnecting && self.options.reconnect) { - setTimeout(function () { + if (!self.disconnecting && self.options.reconnect && !self.reconnectTimeout) { + self.reconnectTimeout = setTimeout(function () { + self.reconnectTimeout = null; self.connect(); self.emit("reconnecting"); }, (self.options.reconnectInterval * 1000)); } @@ -84,8 +86,9 @@ module.exports = function (ccg) { self.connected = false; client = false; - if (!self.disconnecting && self.options.reconnect) { - setTimeout(function () { + if (!self.disconnecting && self.options.reconnect && !self.reconnectTimeout) { + self.reconnectTimeout = setTimeout(function () { + self.reconnectTimeout = null; self.connect(); self.emit("reconnecting"); }, (self.options.reconnectInterval * 1000)); }