mirror of https://github.com/waku-org/js-waku.git
Implement /peers and /connect commands
This commit is contained in:
parent
2297d09d58
commit
80bf4eb4ea
|
@ -24,7 +24,14 @@ export default function App() {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function initWaku() {
|
async function initWaku() {
|
||||||
try {
|
try {
|
||||||
const waku = await Waku.create({ config: { pubsub: { enabled: true, emitSelf: true } } });
|
const waku = await Waku.create({
|
||||||
|
config: {
|
||||||
|
pubsub: {
|
||||||
|
enabled: true,
|
||||||
|
emitSelf: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
setState(({ messages }) => {
|
setState(({ messages }) => {
|
||||||
return { waku, messages };
|
return { waku, messages };
|
||||||
|
@ -67,21 +74,67 @@ export default function App() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const commandHandler = (cmd: string) => {
|
const commandHandler = (input: string) => {
|
||||||
let commandResponse = 'internal error';
|
let commandResponses: string[] = [];
|
||||||
|
const args = input.split(' ');
|
||||||
|
const cmd = args.shift()!;
|
||||||
|
const waku = state.waku;
|
||||||
|
if (!waku) {
|
||||||
|
commandResponses.push('Waku is not yet initialized');
|
||||||
|
} else {
|
||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
case '/help':
|
case '/help':
|
||||||
commandResponse = '/help Display this help';
|
commandResponses.push('/connect <Multiaddr>: connect to the given peer');
|
||||||
|
commandResponses.push('/help: Display this help');
|
||||||
|
break;
|
||||||
|
case '/connect':
|
||||||
|
const peer = args.shift();
|
||||||
|
if (!peer) {
|
||||||
|
commandResponses.push('No peer provided');
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
const peerMultiaddr = multiaddr(peer);
|
||||||
|
const peerId = peerMultiaddr.getPeerId();
|
||||||
|
if (!peerId) {
|
||||||
|
commandResponses.push('Peer Id needed to dial');
|
||||||
|
} else {
|
||||||
|
waku.libp2p.peerStore.addressBook.add(
|
||||||
|
PeerId.createFromB58String(peerId),
|
||||||
|
[peerMultiaddr]);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
commandResponses.push('Invalid multaddr: ' + e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case '/peers':
|
||||||
|
waku.libp2p.peerStore.peers.forEach((peer, peerId) => {
|
||||||
|
commandResponses.push(peerId + ":")
|
||||||
|
let addresses = " addresses: ["
|
||||||
|
peer.addresses.forEach(({multiaddr}) => {
|
||||||
|
addresses += " " + multiaddr.toString() + ",";
|
||||||
|
})
|
||||||
|
addresses = addresses.replace(/,$/,"");
|
||||||
|
addresses += "]";
|
||||||
|
commandResponses.push(addresses);
|
||||||
|
let protos = " protos: [";
|
||||||
|
protos += peer.protocols;
|
||||||
|
protos+= "]"
|
||||||
|
commandResponses.push(protos)
|
||||||
|
})
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
commandResponse = 'Unknown Command'
|
commandResponses.push('Unknown Command');
|
||||||
}
|
}
|
||||||
|
|
||||||
setState(({waku, messages}) => {
|
|
||||||
messages.push(new ChatMessage(new Date(), 'Command Response', commandResponse));
|
|
||||||
return {waku, messages};
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
setState(({ waku, messages }) => {
|
||||||
|
commandResponses.forEach((res) => {
|
||||||
|
messages.push(new ChatMessage(new Date(), cmd, res));
|
||||||
|
});
|
||||||
|
return { waku, messages };
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className='App'>
|
<div className='App'>
|
||||||
|
|
Loading…
Reference in New Issue