close clients first (before tunneler) on exit

This commit is contained in:
AJ ONeal 2016-10-06 00:00:41 -06:00
parent 1de642efa4
commit a13a4e62b2
1 changed files with 22 additions and 16 deletions

View File

@ -21,7 +21,6 @@ function run(copts) {
var token = copts.token; var token = copts.token;
var tunnelUrl = copts.stunneld.replace(/\/$/, '') + '/?access_token=' + token; var tunnelUrl = copts.stunneld.replace(/\/$/, '') + '/?access_token=' + token;
var wstunneler; var wstunneler;
var retry = true;
var localclients = {}; var localclients = {};
// BaaS / Backendless / noBackend / horizon.io // BaaS / Backendless / noBackend / horizon.io
// user authentication // user authentication
@ -141,20 +140,8 @@ function run(copts) {
console.info("[open] connected to '" + copts.stunneld + "'"); console.info("[open] connected to '" + copts.stunneld + "'");
} }
, onClose: function () { , retry: true
if (!authenticated) { , closeClients: function () {
console.info('[close] failed on first attempt... check authentication.');
}
else if (retry) {
console.info('[retry] disconnected and waiting...');
setTimeout(run, 5000, copts);
}
else {
console.info('[close] closing tunnel to exit...');
}
process.removeListener('exit', wsHandlers.onExit);
process.removeListener('SIGINT', wsHandlers.onExit);
Object.keys(localclients).forEach(function (cid) { Object.keys(localclients).forEach(function (cid) {
try { try {
localclients[cid].end(); localclients[cid].end();
@ -165,13 +152,32 @@ function run(copts) {
}); });
} }
, onClose: function () {
if (!authenticated) {
console.info('[close] failed on first attempt... check authentication.');
}
else if (wsHandlers.retry) {
console.info('[retry] disconnected and waiting...');
setTimeout(run, 5000, copts);
}
else {
console.info('[close] closing tunnel to exit...');
}
process.removeListener('exit', wsHandlers.onExit);
process.removeListener('SIGINT', wsHandlers.onExit);
wsHandlers.closeClients();
}
, onError: function (err) { , onError: function (err) {
console.error("[tunnel error] " + err.message); console.error("[tunnel error] " + err.message);
console.error(err); console.error(err);
} }
, onExit: function () { , onExit: function () {
retry = false; console.log('[wait] closing wstunneler...');
wsHandlers.retry = false;
wsHandlers.closeClients();
try { try {
wstunneler.close(); wstunneler.close();
} catch(e) { } catch(e) {