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,28 +74,74 @@ export default function App() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const commandHandler = (cmd: string) => {
|
const commandHandler = (input: string) => {
|
||||||
let commandResponse = 'internal error';
|
let commandResponses: string[] = [];
|
||||||
switch (cmd) {
|
const args = input.split(' ');
|
||||||
case '/help':
|
const cmd = args.shift()!;
|
||||||
commandResponse = '/help Display this help';
|
const waku = state.waku;
|
||||||
break;
|
if (!waku) {
|
||||||
default:
|
commandResponses.push('Waku is not yet initialized');
|
||||||
commandResponse = 'Unknown Command'
|
} else {
|
||||||
}
|
switch (cmd) {
|
||||||
|
case '/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;
|
||||||
|
default:
|
||||||
|
commandResponses.push('Unknown Command');
|
||||||
|
}
|
||||||
|
|
||||||
setState(({waku, messages}) => {
|
}
|
||||||
messages.push(new ChatMessage(new Date(), 'Command Response', commandResponse));
|
setState(({ waku, messages }) => {
|
||||||
return {waku, messages};
|
commandResponses.forEach((res) => {
|
||||||
})
|
messages.push(new ChatMessage(new Date(), cmd, res));
|
||||||
}
|
});
|
||||||
|
return { waku, messages };
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className='App'>
|
<div className='App'>
|
||||||
<div className='chat-room'>
|
<div className='chat-room'>
|
||||||
<WakuContext.Provider value={{ waku: state.waku }}>
|
<WakuContext.Provider value={{ waku: state.waku }}>
|
||||||
<Paper>
|
<Paper>
|
||||||
<Room lines={state.messages} commandHandler={commandHandler}/>
|
<Room lines={state.messages} commandHandler={commandHandler} />
|
||||||
</Paper>
|
</Paper>
|
||||||
</WakuContext.Provider>
|
</WakuContext.Provider>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue