Created
August 22, 2019 18:34
-
-
Save Pierozi/cbf4a3ddb7f30c24a1f81889f4f53bd1 to your computer and use it in GitHub Desktop.
LibP2P Listener Circuit Relay
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
/* eslint-disable no-console */ | |
const relayId = "QmemquW1rBrxZ4nq1KvNSKgHiRqKhXQdcW4ZvWSMftVAuy" | |
const relayPort = "54783" | |
const Node = require('./node-circuit') | |
const PeerInfo = require('peer-info') | |
const PeerBook = require('peer-book') | |
const PeerId = require('peer-id') | |
const peerBook = new PeerBook() | |
const relayPeerId = PeerId.createFromB58String(relayId) | |
const relayPeerInfo = new PeerInfo(relayPeerId) | |
relayPeerInfo.multiaddrs.add(`/ip4/127.0.0.1/tcp/${relayPort}`) | |
peerBook.put(relayPeerInfo) | |
PeerInfo.create((err, peerInfo) => { | |
console.log('Peer ID: ', peerInfo.id.toB58String()) | |
const listenerNode = new Node({ | |
peerInfo: peerInfo, | |
peerBook: peerBook, | |
}); | |
listenerNode.on('peer:connect', (peerInfo) => { | |
console.log('received dial to me from:', peerInfo.id.toB58String()); | |
}); | |
listenerNode.handle('/echo/1.0.0', (protocol, conn) => { | |
console.log('callback handle'); | |
pull( | |
conn, | |
pull.map((data) => { | |
return data.toString('utf8').replace('\n', '') | |
}), | |
pull.drain(console.log) | |
) | |
}); | |
listenerNode.start(() => { | |
console.log('callback start') | |
}); | |
console.log('Listener ready, listening on:'); | |
listenerNode.peerInfo.multiaddrs.forEach((ma) => { | |
console.log(ma.toString()) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment