-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from diasdavid/example/using-swarm
using swarm example
- Loading branch information
Showing
5 changed files
with
76 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Using libp2p-swarm | ||
================== | ||
|
||
|
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,37 @@ | ||
var Swarm = require('libp2p-swarm') | ||
var tcp = require('libp2p-tcp') | ||
var multiaddr = require('multiaddr') | ||
var Id = require('peer-id') | ||
var Spdy = require('libp2p-spdy') | ||
var Libp2p = require('../../src') | ||
var Peer = require('peer-info') | ||
|
||
// set up | ||
|
||
var mh = multiaddr('/ip4/127.0.0.1/tcp/8010') | ||
var p = new Peer(Id.create(), []) | ||
var sw = new Swarm(p) | ||
|
||
// create a libp2p node | ||
|
||
var node = new Libp2p(sw) | ||
|
||
node.swarm.addTransport('tcp', tcp, {multiaddr: mh}, {}, {port: 8010}, function () { | ||
// Ready to receive incoming connections | ||
|
||
sw.addStreamMuxer('spdy', Spdy, {}) | ||
|
||
// dial to another node | ||
|
||
var mh2 = multiaddr('/ip4/127.0.0.1/tcp/8020') | ||
var p2 = new Peer(Id.create(), [mh2]) | ||
|
||
node.swarm.dial(p2, {}, '/sparkles/1.0.0', function (err, conn) { | ||
if (err) { | ||
return console.error(err) | ||
} | ||
|
||
console.log('-> connection is ready') | ||
process.stdin.pipe(conn).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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
var Swarm = require('libp2p-swarm') | ||
var tcp = require('libp2p-tcp') | ||
var multiaddr = require('multiaddr') | ||
var Id = require('peer-id') | ||
var Spdy = require('libp2p-spdy') | ||
var Libp2p = require('../../src') | ||
var Peer = require('peer-info') | ||
|
||
// set up | ||
|
||
var mh = multiaddr('/ip4/127.0.0.1/tcp/8020') | ||
var p = new Peer(Id.create(), []) | ||
var sw = new Swarm(p) | ||
|
||
sw.addTransport('tcp', tcp, {multiaddr: mh}, {}, {port: 8020}, function () { | ||
// Ready to receive incoming connections | ||
|
||
sw.addStreamMuxer('spdy', Spdy, {}) | ||
|
||
// create a libp2p node | ||
|
||
var node = new Libp2p(sw) | ||
|
||
// handle/mount a protocol | ||
|
||
node.swarm.handleProtocol('/sparkles/1.0.0', function (conn) { | ||
process.stdin.pipe(conn).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
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
exports = module.exports = Libp2p | ||
|
||
function Libp2p (swarm, peerRouting, recordStore) { | ||
|