From 7d1c55314925b0c71776f3fce4c57ba350e680ac Mon Sep 17 00:00:00 2001 From: John Shaver Date: Thu, 14 Jun 2018 14:57:09 -0700 Subject: [PATCH] Added open/close/error/disconenct/reconenct handlers to remote. --- lib/remote.js | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/remote.js b/lib/remote.js index c2f8c07..bdc35c3 100644 --- a/lib/remote.js +++ b/lib/remote.js @@ -33,6 +33,7 @@ function _connect(state) { var wstunneler; var authenticated = false; var authsent = false; + var initialConnect = true; var localclients = {}; var pausedClients = []; @@ -404,6 +405,7 @@ function _connect(state) { , onOpen: function () { console.info("[open] connected to '" + (state.wss || state.relay) + "'"); wsHandlers.refreshTimeout(); + timeoutId = setTimeout(wsHandlers.checkTimeout, activityTimeout); wstunneler._socket.on('drain', function () { @@ -425,6 +427,14 @@ function _connect(state) { pausedClients.length = 0; }); + + //Call either Open or Reconnect handlers. + if(state.handlers.onOpen && initialConnect) { + state.handlers.onOpen(); + } else if (state.handlers.onReconnect && !initialConnect) { + state.handlers.onReconnect(); + } + intitialConnect = false; } , onClose: function () { @@ -443,22 +453,38 @@ function _connect(state) { } if (!authenticated) { + if(state.handlers.onError) { + let err = new Error('Failed to connect on first attempt... check authentication'); + state.handlers.onError(err); + } + if(state.handlers.onClose) { + state.handlers.onClose() + } console.info('[close] failed on first attempt... check authentication.'); timeoutId = null; } else if (tokens.length) { + if(state.handlers.onDisconnect) { + state.handlers.onDisconnect(); + } console.info('[retry] disconnected and waiting...'); timeoutId = setTimeout(connect, 5000); + } else { + if(state.handlers.onClose) { + state.handlers.onClose() + } } + } , onError: function (err) { - console.error("[tunnel error] " + err.message); - console.error(err); - if (connCallback) { - connCallback(err); - } + console.error("[tunnel error] " + err.message); + console.error(err); + + if (connCallback) { + connCallback(err); } + } , sendMessage: function (msg) { if (wstunneler) {