From e02bab294887db159ac04988431437dc107cb73a Mon Sep 17 00:00:00 2001 From: Bronwen Date: Sun, 14 Feb 2021 01:11:58 -0500 Subject: [PATCH] retry connection backoff --- relay/index.js | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/relay/index.js b/relay/index.js index f0e48c7..2da8804 100644 --- a/relay/index.js +++ b/relay/index.js @@ -7,11 +7,14 @@ const { Identity } = require('../lib/Identity'); const stp = require('../lib/STP'); title('relay', false); const identity = await new Identity('relay', 'default'); +const upnp = require('../lib/upnp'); const clients = []; // const client = stp.connect(identity, config.ports.relay, '127.0.0.1'); +// upnp.mapIndefinite(5600); + // ==================================== [STP SERVER] stp.createServer({ identity: identity, @@ -21,14 +24,26 @@ stp.createServer({ clients.push(socket); }); -const client = stp.connect({ - identity, - port: config.ports.relay, - ip: 'valnet.xyz' -}); -client.on('error', e => { - log.error(e) -}) +function connectNetwork(t = 1000) { + if(t > 65000) t /= 2; + + const client = stp.connect({ + identity, + port: config.ports.relay, + ip: 'valnet.xyz' + }); + client.on('ready', () => { + log.success('connectd!'); + t = 500; + }) + client.on('error', e => { + }); + client.on('close', e => { + setTimeout(connectNetwork.bind(global, t*2), t); + log.debug('retrying connection... ' + (t/1000) + 's') + }); +} +connectNetwork(); // ==================================== [EXPRESS] const express = require('express');