Created
November 20, 2012 07:09
-
-
Save Raynos/4116515 to your computer and use it in GitHub Desktop.
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
var WriteStream = require("write-stream") | |
, uuid = require("node-uuid") | |
, assert = require("assert") | |
// A pool of open peer connections. Used to connect to other people | |
, PeerConnectionPool = require("peer-connection-pool") | |
// A list of all peers in the network | |
, Peers = require("peer-nodes") | |
, peers = Peers() | |
, id = uuid() | |
// When a new peer connection opens you get a stream! | |
, pool = PeerConnectionPool(function (stream) { | |
// I am an echo stream | |
stream.pipe(stream) | |
}).listen(id) | |
// new peer in the network? Awesome connect to them! | |
peers.on("join", function (peer) { | |
var stream = pool.connect(peer.id) | |
stream.pipe(WriteStream(function onwrite(message) { | |
assert.equal(message, "hello world") | |
})) | |
stream.write("hello world") | |
}) | |
// join the network! | |
peers.join({ | |
id: id | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment