Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This file is used to configure and run a public node.
- The public node is used to listen/accept incoming connections.
- This public node can be reached by other nodes behind a NAT.
- */
- import { createLibp2p } from 'libp2p'
- import { webSockets } from '@libp2p/websockets'
- import { noise } from '@chainsafe/libp2p-noise'
- import { mplex } from '@libp2p/mplex'
- async function main ()
- {
- const myNode = await createLibp2p({
- addresses: { listen: ['/ip4/92.185.24.206/tcp/0/ws', '/ip4/127.0.0.1/tcp/0/ws'] },
- transports: [ webSockets() ],
- connectionEncryption: [ noise() ],
- streamMuxers: [ mplex() ]
- })
- function connectToNode(addr)
- {
- // Try to connect to the node and return if the connection was successful.
- try
- {
- myNode.dial(addr)
- console.log("Connected to " + addr)
- }
- catch (e)
- {
- console.log("Failed to connect to " + addr)
- console.error(e)
- return false
- }
- }
- await myNode.start()
- .then(() => console.log("Node ID: " + myNode.peerId.toString())) // Display the Node ID
- .then(() =>
- {
- console.log("Listening:") // Display the listening addresses.
- myNode.getMultiaddrs().forEach((ma) => console.log("\t" + ma.toString()))
- })
- .then(() =>
- {
- connectToNode("/ip4/92.185.24.206/tcp/38353/ws/p2p/12D3KooWJiv6N9wSGaXnHj9wjJvd3S6aD9khijubpfoFBHGgo6B9")
- })
- .catch((err) => console.error(err))
- }
- main().then(r => console.log(r)).catch(e => console.error(e))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement