diff --git a/bin/telebitd.js b/bin/telebitd.js index 0183524..a7dd116 100755 --- a/bin/telebitd.js +++ b/bin/telebitd.js @@ -509,7 +509,7 @@ function connectTunnel() { }; state.greenlockConf = state.config.greenlock || {}; - state.sortingHat = state.config.sortingHat || path.resolve(__dirname, '..', 'lib/sorting-hat.js'); + state.sortingHat = state.config.sortingHat; // TODO sortingHat.print(); ? diff --git a/lib/remote.js b/lib/remote.js index 3af3709..4a75363 100644 --- a/lib/remote.js +++ b/lib/remote.js @@ -23,6 +23,9 @@ function _connect(state) { // they can get a promise that will provide feedback about invalid tokens. var tokens = []; var auth; + if(!state.sortingHat) { + state.sortingHat = "./sorting-hat.js"; + } if (state.token) { tokens.push(state.token); } @@ -30,6 +33,7 @@ function _connect(state) { var wstunneler; var authenticated = false; var authsent = false; + var initialConnect = true; var localclients = {}; var pausedClients = []; @@ -401,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 () { @@ -422,10 +427,17 @@ 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(); + } + initialConnect = false; } , onClose: function () { - console.log('ON CLOSE'); clearTimeout(timeoutId); wstunneler = null; clientHandlers.closeAll(); @@ -440,12 +452,26 @@ 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() + } } }