Skip to content

Commit

Permalink
Improve console logs on peer:connected event (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaCWebDeveloper committed Nov 11, 2022
1 parent df5f8dd commit 4d391cc
Showing 1 changed file with 50 additions and 35 deletions.
85 changes: 50 additions & 35 deletions packages/gateway-client/src/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -842,47 +842,62 @@ async function main() {
node.connectionManager.addEventListener(
'peer:connect',
async evt => {
// log connection details
const connection = evt.detail;
const str_id = connection.remotePeer.toString();
console.log(`Connected to ${str_id}, check ${serverID}`);
console.log(`Connected to: `, {
server: str_id,
via: connection.remoteAddr.toString(),
});
// set keep-alive on connection
await node.peerStore
.tagPeer(connection.remotePeer, KEEP_ALIVE)
.catch(_ => null);
if (str_id === serverID) {
// update status

self.serverPeer = connection.remotePeer;
self.status.serverPeer = ServerPeerStatus.CONNECTED;

if (Date.now() - relayDebounce > 60000) {
relayDebounce = Date.now();
openRelayStream(() => {
/*
* Don't wait for a relay before resolving.
*
* A relay is required in order to access the box
* outside of the box's LAN. There are currently
* two methods of obtaining a relay: a UPnP address
* on the local network and a public UPnP address
* on the SamizdApp network; however, both methods
* are currently unreliable.
*
* Until we have a way of reliably obtaining a
* public relay, do not wait for a public relay
* before resolving.
*
* TODO: Strengthen one of the methods for
* obtaining a public relay address.
*
*/
//resolve();
});
}

// TODO: Don't resolve here once we have a reliable way
// of obtaining a public relay
resolve();

// if this is not our server
const serverMatch = str_id === serverID;
console.log(
`Server match: ${serverMatch} (${str_id} ${
serverMatch ? '=' : '!'
}== ${serverID})`
);
if (!serverMatch) {
// then there is no more to do
return;
} // else, we've connected to our server

// update status
self.serverPeer = connection.remotePeer;
self.status.serverPeer = ServerPeerStatus.CONNECTED;

if (Date.now() - relayDebounce > 60000) {
relayDebounce = Date.now();
openRelayStream(() => {
/*
* Don't wait for a relay before resolving.
*
* A relay is required in order to access the box
* outside of the box's LAN. There are currently
* two methods of obtaining a relay: a UPnP address
* on the local network and a public UPnP address
* on the SamizdApp network; however, both methods
* are currently unreliable.
*
* Until we have a way of reliably obtaining a
* public relay, do not wait for a public relay
* before resolving.
*
* TODO: Strengthen one of the methods for
* obtaining a public relay address.
*
*/
//resolve();
});
}

// TODO: Don't resolve here once we have a reliable way
// of obtaining a public relay
resolve();
}
);
} catch (e) {
Expand Down

0 comments on commit 4d391cc

Please sign in to comment.