Last active
August 4, 2021 22:49
-
-
Save lanmower/23050339cb96ef461b0d8a766b35e58d to your computer and use it in GitHub Desktop.
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
const DHT = require("@hyperswarm/dht"); | |
var pump = require("pump"); | |
var fs = require("fs"); | |
const crypto = require("hypercore-crypto"); | |
//const keyPair = crypto.keyPair(); | |
(async () => { | |
const node = new DHT(); | |
const keyPair = DHT.keyPair(); | |
await node.ready(); | |
const hash = crypto.data(Buffer.from("test", "utf-8")); | |
node.announce(hash, keyPair); | |
const server = node.createServer(); | |
console.log("announcing", hash.toString("hex")); | |
server.on("connection", function(noiseSocket) { | |
var source = fs.createReadStream("package.json"); | |
pump(source, noiseSocket); | |
}); | |
console.log("starting server"); | |
server.listen(keyPair); | |
setTimeout(async ()=>{ | |
const anotherNode = new DHT(); | |
await anotherNode.ready(); | |
console.log("finding", hash.toString("hex")); | |
for await (const n of await anotherNode.lookup(hash)) { | |
console.log("found"); | |
const noiseSocket = await anotherNode.connect(n.id); | |
pump(noiseSocket, process.stdout); | |
} | |
console.log("done finding"); | |
},10000) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment