mirror of
https://github.com/cerc-io/watcher-ts
synced 2026-09-08 16:54:08 +00:00
Use webrtc-direct protocol with peer signalling integrated into relay node (#307)
* Use webrtc-direct transport with pubsub based discovery * Use changes to integrate signalling using relay node * Add an indicator for peer connection type (direct/relayed) * Catch errors while creting a libp2p node * Catch errors while dialling peers * Catch errors when dialling from relay node * Pass CLI arg for port to listen on for relay node * Subscribe chat CLI to pubsub topic * Update yarn lockfile * Update webrtc-direct dependency in package json * Update webrtc-direct version * Update yarn lockfile
This commit is contained in:
committed by
Ashwin Phatak
parent
453ee6a473
commit
a1af962d99
@@ -9,26 +9,27 @@ import yargs from 'yargs';
|
||||
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/49721#issuecomment-1319854183
|
||||
import { PeerId } from '@libp2p/interface-peer-id';
|
||||
|
||||
const TEST_TOPIC = 'test';
|
||||
interface Arguments {
|
||||
signalServer: string;
|
||||
relayNode: string;
|
||||
}
|
||||
|
||||
async function main (): Promise<void> {
|
||||
const argv: Arguments = _getArgv();
|
||||
if (!argv.signalServer) {
|
||||
console.log('Using the default signalling server URL');
|
||||
}
|
||||
|
||||
// https://adamcoster.com/blog/commonjs-and-esm-importexport-compatibility-examples#importing-esm-into-commonjs-cjs
|
||||
const { Peer } = await import('@cerc-io/peer');
|
||||
const peer = new Peer(true);
|
||||
await peer.init(argv.signalServer, argv.relayNode);
|
||||
const peer = new Peer(argv.relayNode, true);
|
||||
await peer.init();
|
||||
|
||||
peer.subscribeMessage((peerId: PeerId, message: string) => {
|
||||
console.log(`> ${peerId.toString()} > ${message}`);
|
||||
});
|
||||
|
||||
peer.subscribeTopic(TEST_TOPIC, (peerId, data) => {
|
||||
console.log(`> ${peerId.toString()} > ${data}`);
|
||||
});
|
||||
|
||||
console.log(`Peer ID: ${peer.peerId?.toString()}`);
|
||||
|
||||
const rl = readline.createInterface({
|
||||
@@ -47,13 +48,10 @@ function _getArgv (): any {
|
||||
return yargs(hideBin(process.argv)).parserConfiguration({
|
||||
'parse-numbers': false
|
||||
}).options({
|
||||
signalServer: {
|
||||
type: 'string',
|
||||
describe: 'Signalling server URL'
|
||||
},
|
||||
relayNode: {
|
||||
type: 'string',
|
||||
describe: 'Relay node URL'
|
||||
describe: 'Relay node URL',
|
||||
demandOption: true
|
||||
}
|
||||
}).argv;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user