Add /connections command

This commit is contained in:
Franck Royer 2021-05-04 16:38:38 +10:00
parent 4a722dc144
commit 65404c769a
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 28 additions and 0 deletions

View File

@ -78,6 +78,31 @@ function peers(waku: Waku | undefined): string[] {
return response;
}
function connections(waku: Waku | undefined): string[] {
if (!waku) {
return ['Waku node is starting'];
}
let response: string[] = [];
waku.libp2p.connections.forEach(
(
connections: import('libp2p-interfaces/src/connection/connection')[],
peerId
) => {
response.push(peerId + ':');
let strConnections = ' connections: [';
connections.forEach((connection) => {
strConnections += JSON.stringify(connection.stat);
});
strConnections += ']';
response.push(strConnections);
}
);
if (response.length === 0) {
response.push('Not connected to any peer.');
}
return response;
}
export default function handleCommand(
input: string,
waku: Waku | undefined,
@ -102,6 +127,9 @@ export default function handleCommand(
case '/peers':
peers(waku).map((str) => response.push(str));
break;
case '/connections':
connections(waku).map((str) => response.push(str));
break;
default:
response.push(`Unknown Command '${command}'`);
}