forked from datrs/hyperswarm-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrote the implementation and added a README.
- Loading branch information
Showing
20 changed files
with
1,405 additions
and
297 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ target/ | |
tmp/ | ||
Cargo.lock | ||
.DS_Store | ||
SANDBOX |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
yarn.lock | ||
package-lock.json |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "js", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"dependencies": { | ||
"hyperswarm": "^2.15.3" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const hyperswarm = require('hyperswarm') | ||
|
||
const opts = { | ||
runBootstrap: false | ||
} | ||
main(opts).catch(console.error) | ||
|
||
async function main (opts = {}) { | ||
let bootstrap | ||
if (opts.runBootstrap) { | ||
bootstrap = await bootstrapDHT(6060) | ||
} else { | ||
bootstrap = 'localhost:6060' | ||
} | ||
console.log({ bootstrap }) | ||
|
||
const topic = Buffer.alloc(32, 0) | ||
|
||
const swarm1 = runNode(bootstrap, 'node1') | ||
const swarm2 = runNode(bootstrap, 'node2') | ||
|
||
const config = { announce: true, lookup: true } | ||
swarm1.join(topic, config) | ||
swarm2.join(topic, config) | ||
} | ||
|
||
function runNode (bootstrap, name) { | ||
const swarm = hyperswarm({ | ||
announceLocalAddress: true, | ||
bootstrap: [bootstrap] | ||
}) | ||
|
||
swarm.on('connection', (socket, info) => { | ||
const peer = info.peer | ||
let peerAddr = peer ? `${peer.host}:${peer.port}` : 'unknown' | ||
const label = `[${name} -> ${info.type}://${peerAddr}]` | ||
console.log(`${label} connect`) | ||
socket.write(Buffer.from(`hi from ${name}!`)) | ||
socket.on('data', buf => { | ||
console.log(`${label} read: ${buf.toString()}`) | ||
}) | ||
socket.on('error', err => { | ||
console.log(`${label} error: ${err.toString()}`) | ||
}) | ||
socket.on('close', () => { | ||
console.log(`${label} close`) | ||
}) | ||
}) | ||
|
||
return swarm | ||
} | ||
|
||
async function bootstrapDHT (port) { | ||
const bootstrapper = require('@hyperswarm/dht')({ | ||
bootstrap: false | ||
}) | ||
bootstrapper.listen(port) | ||
await new Promise(resolve => { | ||
return bootstrapper.once('listening', resolve) | ||
}) | ||
const bootstrapPort = bootstrapper.address().port | ||
const bootstrapAddr = `localhost:${bootstrapPort}` | ||
console.log(`bootstrap node running on ${bootstrapAddr}`) | ||
return bootstrapAddr | ||
} |
This file contains 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
Oops, something went wrong.