handle sigint

This commit is contained in:
AJ ONeal 2016-09-30 01:46:00 -04:00
parent 71b3815c52
commit 11546b298a
1 changed files with 34 additions and 5 deletions

View File

@ -40,13 +40,15 @@ return;
//*/
var tunnelUrl = 'wss://pokemap.hellabit.com:3000/?access_token=' + token;
var wstunneler;
function run() {
var wstunneler = new WebSocket(tunnelUrl, { rejectUnauthorized: false });
var retry = true;
var localclients = {};
wstunneler = new WebSocket(tunnelUrl, { rejectUnauthorized: false });
function onOpen() {
console.log('[open] tunneler connected');
var localclients = {};
/*
setInterval(function () {
@ -163,16 +165,43 @@ return;
wstunneler.on('open', onOpen);
wstunneler.on('close', function () {
console.log('retry on close');
setTimeout(run, 5000);
console.log('closing tunnel...');
process.removeListener('exit', onExit);
Object.keys(localclients).forEach(function (cid) {
try {
localclients[cid].end();
} catch(e) {
// ignore
}
delete localclients[cid];
});
if (retry) {
console.log('retry on close');
setTimeout(run, 5000);
}
});
wstunneler.on('error', function (err) {
console.error("[error] will retry on 'close'");
console.error(err);
});
function onExit() {
retry = false;
console.log('on exit...');
try {
wstunneler.close();
} catch(e) {
console.error(e);
// ignore
}
}
process.on('exit', onExit);
process.on('SIGINT', onExit);
}
run();
}());