better handling and error handling
This commit is contained in:
parent
1bdfad6691
commit
71b3815c52
71
wsclient.js
71
wsclient.js
|
@ -39,8 +39,12 @@ request.get('https://pokemap.hellabit.com:3000?access_token=' + token, { rejectU
|
|||
return;
|
||||
//*/
|
||||
|
||||
var wstunneler = new WebSocket('wss://pokemap.hellabit.com:3000/?access_token=' + token, { rejectUnauthorized: false });
|
||||
wstunneler.on('open', function () {
|
||||
var tunnelUrl = 'wss://pokemap.hellabit.com:3000/?access_token=' + token;
|
||||
|
||||
function run() {
|
||||
var wstunneler = new WebSocket(tunnelUrl, { rejectUnauthorized: false });
|
||||
|
||||
function onOpen() {
|
||||
console.log('[open] tunneler connected');
|
||||
var localclients = {};
|
||||
|
||||
|
@ -60,8 +64,8 @@ wstunneler.on('open', function () {
|
|||
// file management
|
||||
// Synergy Teamwork Paradigm = Jabberwocky
|
||||
var pack = require('tunnel-packer').pack;
|
||||
|
||||
function onMessage(opts) {
|
||||
var handlers = {
|
||||
onmessage: function (opts) {
|
||||
var cid = addrToId(opts);
|
||||
console.log('[wsclient] onMessage:', cid);
|
||||
var service = opts.service;
|
||||
|
@ -72,14 +76,12 @@ wstunneler.on('open', function () {
|
|||
var m;
|
||||
|
||||
if (opts.data.byteLength < 20) {
|
||||
if ('|__ERROR__|' === opts.data.toString('ascii')
|
||||
|| '|__END__|' === opts.data.toString('ascii')) {
|
||||
|
||||
console.log("['" + opts.data.toString('ascii') + "'] '" + cid + "'");
|
||||
if (localclients[cid]) {
|
||||
localclients[cid].end();
|
||||
delete localclients[cid];
|
||||
if ('|__ERROR__|' === opts.data.toString('ascii')) {
|
||||
handlers.onerror(opts);
|
||||
return;
|
||||
}
|
||||
else if ('|__END__|' === opts.data.toString('ascii')) {
|
||||
handlers.onend(opts);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -116,7 +118,9 @@ wstunneler.on('open', function () {
|
|||
console.log("servername: '" + servername + "'");
|
||||
|
||||
lclient = localclients[cid] = net.createConnection({ port: port, host: '127.0.0.1' }, function () {
|
||||
|
||||
console.log("[=>] first packet from tunneler to '" + cid + "' as '" + opts.service + "'", opts.data.byteLength);
|
||||
lclient.write(opts.data);
|
||||
});
|
||||
lclient.on('data', function (chunk) {
|
||||
console.log("[<=] local '" + opts.service + "' sent to '" + cid + "' <= ", chunk.byteLength, "bytes");
|
||||
console.log(JSON.stringify(chunk.toString()));
|
||||
|
@ -133,19 +137,42 @@ wstunneler.on('open', function () {
|
|||
delete localclients[cid];
|
||||
wstunneler.send(pack(opts, Buffer.from('|__END__|'), 'end'), { binary: true });
|
||||
});
|
||||
}
|
||||
, onend: function (opts) {
|
||||
var cid = addrToId(opts);
|
||||
console.log("[end] '" + cid + "'");
|
||||
handlers._onend(cid, opts);
|
||||
}
|
||||
, onerror: function (opts) {
|
||||
var cid = addrToId(opts);
|
||||
console.log("[error] '" + cid + "'", opts.code || '', opts.message);
|
||||
handlers._onend(cid);
|
||||
}
|
||||
, _onend: function (cid) {
|
||||
if (localclients[cid]) {
|
||||
localclients[cid].end();
|
||||
}
|
||||
delete localclients[cid];
|
||||
}
|
||||
};
|
||||
var machine = require('tunnel-packer').create(handlers);
|
||||
|
||||
console.log("[=>] first packet from tunneler to '" + cid + "' as '" + opts.service + "'", opts.data.byteLength);
|
||||
lclient.write(opts.data);
|
||||
wstunneler.on('message', machine.fns.addChunk);
|
||||
}
|
||||
|
||||
wstunneler.on('open', onOpen);
|
||||
|
||||
wstunneler.on('close', function () {
|
||||
console.log('retry on close');
|
||||
setTimeout(run, 5000);
|
||||
});
|
||||
|
||||
wstunneler.on('error', function (err) {
|
||||
console.error("[error] will retry on 'close'");
|
||||
console.error(err);
|
||||
});
|
||||
}
|
||||
|
||||
var machine = require('tunnel-packer').create({ onMessage: onMessage });
|
||||
|
||||
wstunneler.on('message', machine.fns.addChunk);
|
||||
|
||||
wstunneler.on('close', function () {
|
||||
console.log('end');
|
||||
});
|
||||
});
|
||||
run();
|
||||
|
||||
}());
|
||||
|
|
Loading…
Reference in New Issue