Do not fail if connection to one peer fails

As long as we connect to at least one peer then we can move forward.
This commit is contained in:
Franck Royer 2021-07-09 14:23:42 +10:00
parent 66d450ae36
commit d00c1c874a
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 18 additions and 11 deletions

View File

@ -1,5 +1,5 @@
import { Dispatch, SetStateAction } from 'react';
import { Environment, getStatusFleetNodes, Waku, WakuMessage } from 'js-waku';
import { getStatusFleetNodes, Waku, WakuMessage } from 'js-waku';
import { decode, DirectMessage, PublicKeyMessage } from './messaging/wire';
import { decryptMessage, validatePublicKeyMessage } from './crypto';
import { Message } from './messaging/Messages';
@ -11,20 +11,27 @@ export const DirectMessageContentTopic = '/eth-dm/1/direct-message/json';
export async function initWaku(): Promise<Waku> {
const waku = await Waku.create({});
const nodes = await getNodes();
await Promise.all(
nodes.map((addr) => {
return waku.dial(addr);
})
);
// Dial all nodes it can find
getStatusFleetNodes().then((nodes) => {
nodes.forEach((addr) => {
waku.dial(addr);
});
});
// Wait to be connected to at least one peer
await new Promise((resolve, reject) => {
// If we are not connected to any peer within 10sec let's just reject
// As we are not implementing connection management in this example
setTimeout(reject, 10000);
waku.libp2p.connectionManager.on('peer:connect', () => {
resolve(null);
});
});
return waku;
}
function getNodes() {
return getStatusFleetNodes(Environment.Prod);
}
export function handlePublicKeyMessage(
myAddress: string,
setter: Dispatch<SetStateAction<Map<string, string>>>,