From b1196eb5172b32aa1d3332811ae7f4a8ef5b2451 Mon Sep 17 00:00:00 2001 From: Alex Robsahm Date: Wed, 21 Dec 2016 13:18:07 +0100 Subject: [PATCH] Ping server to keep connection alive --- index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/index.js b/index.js index a54fe3d..0edb749 100644 --- a/index.js +++ b/index.js @@ -8,6 +8,7 @@ const requestConfig = (webSocket, environment) => { }; const initializeConfigUpdate = (conf) => { + let ping; let retryNumOfTimes = 0; const connect = (resolve, reject) => { @@ -16,6 +17,7 @@ const initializeConfigUpdate = (conf) => { webSocket.on("open", () => { retryNumOfTimes = 0; requestConfig(webSocket, conf.environment); + ping = setInterval(ws.ping(), 15000); }); webSocket.on("message", (message) => { @@ -33,6 +35,7 @@ const initializeConfigUpdate = (conf) => { }); webSocket.on("close", () => { + clearInterval(ping); setTimeout(connect, Math.ceil(Math.pow(Math.E, retryNumOfTimes)) * 100, resolve, reject); retryNumOfTimes += 1; });