Created
June 24, 2019 22:21
-
-
Save gitawego/c5dd119364f5bc39ef33c0914d7568d3 to your computer and use it in GitHub Desktop.
hyperswarm
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
'use strict' | |
const { inspect } = require('util') | |
const hyperswarm = require('hyperswarm') | |
const crypto = require('crypto') | |
const swarm = hyperswarm(); | |
const sockets = new WeakMap(); | |
if (!process.argv[2]) { throw Error('node example.js <topic-key>') } | |
const key = crypto.createHash('sha256') | |
.update(process.argv[2]) | |
.digest() | |
swarm.connectivity((err, capabilities) => { | |
console.log('network capabilities', capabilities, err || '') | |
}) | |
swarm.join(key, { | |
announce: true, | |
lookup: true | |
}) | |
swarm.on('connection', function (socket, info) { | |
const { | |
priority, | |
status, | |
retries, | |
peer, | |
client | |
} = info | |
if (peer) { | |
socket.on('data', function (data) { | |
console.log('on data', data); | |
}); | |
socket.on('close', function () { | |
console.log('node2 Connection closed'); | |
}); | |
} else { | |
socket.write('I am node1'); | |
} | |
console.log('new connection!', ` | |
priority: ${priority} | |
status: ${status} | |
retries: ${retries} | |
client: ${client} | |
peer: ${!peer ? peer : ` | |
${inspect(peer, { indentationLvl: 4 }).slice(2, -2)} | |
`} | |
`) | |
if (client) process.stdin.pipe(socket) | |
else socket.pipe(process.stdout) | |
}) |
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
'use strict' | |
const { inspect } = require('util') | |
const hyperswarm = require('hyperswarm') | |
const crypto = require('crypto') | |
const swarm = hyperswarm(); | |
const sockets = new WeakMap(); | |
if (!process.argv[2]) { throw Error('node example.js <topic-key>') } | |
const key = crypto.createHash('sha256') | |
.update(process.argv[2]) | |
.digest() | |
swarm.connectivity((err, capabilities) => { | |
console.log('network capabilities', capabilities, err || '') | |
}) | |
swarm.join(key, { | |
announce: true, | |
lookup: true | |
}) | |
swarm.on('connection', function (socket, info) { | |
const { | |
priority, | |
status, | |
retries, | |
peer, | |
client | |
} = info | |
if (!peer) { | |
socket.on('data', function (data) { | |
console.log('on data', data); | |
}); | |
socket.on('close', function () { | |
console.log('node1 Connection closed'); | |
}); | |
} else { | |
socket.write('I am node2'); | |
} | |
console.log('new connection!', ` | |
priority: ${priority} | |
status: ${status} | |
retries: ${retries} | |
client: ${client} | |
peer: ${!peer ? peer : ` | |
${inspect(peer, { indentationLvl: 4 }).slice(2, -2)} | |
`} | |
`) | |
if (client) process.stdin.pipe(socket) | |
else socket.pipe(process.stdout) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment