Added open/close/error/disconenct/reconenct handlers to remote.

This commit is contained in:
John Shaver 2018-06-14 14:57:09 -07:00
parent 00c973e728
commit 7d1c553149
1 changed files with 31 additions and 5 deletions

View File

@ -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) {