Created
November 21, 2012 06:29
-
-
Save Raynos/4123383 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 net = require("net") | |
, uuid = require("node-uuid") | |
, Peers = require("peer-nodes") | |
, Connection = require("signal-channel/connection") | |
var peers = Peers() | |
, peerStream = Connection("signalchannel.co" | |
, "/v1/echo/") | |
peerStream.pipe(peers.createStream()).pipe(peerStream) | |
function cliqueTopology(options, onConnection) { | |
var id = options.id | |
, connect = options.connect | |
, peers = options.peers | |
options.listen(options.port) | |
.on("connection", function (stream) { | |
var peerId = stream.peerId | |
, peer = peers.get(peerId) | |
onConnection(stream, peer) | |
}) | |
peers.on("join", function (peer) { | |
var peerId = peer.id | |
if (peerId <= id) { | |
// other side handles it | |
return | |
} | |
var stream = options.connect(peerId) | |
onConnection(stream, peer) | |
}) | |
peers.join({ id: id }) | |
} | |
cliqueTopology({ | |
port: 8080 | |
, id: MY PUBLIC IP?! | |
, peers: peers | |
, listen: net.listen | |
, connect: net.connect | |
}, function onConnection(stream) { | |
// do stuff | |
}) | |
// Or P2P | |
/* | |
var PeerConnectionPool = require("peer-connection-pool") | |
, PeerConnectionShim = require("peer-connection-shim") | |
var id = uuid() | |
, relayStream = Connection("signalchannel.co" | |
, "/v1/relay/") | |
, pool = PeerConnectionPool(function () { | |
return PeerConnectionShim({ | |
stream: relayStream | |
}) | |
}) | |
, poolStream = Connection("signalchannel.co" | |
, "/v1/scuttlebutt") | |
poolStream.pipe(pool.createStream()).pipe(poolStream) | |
cliqueTopology({ | |
port: id | |
, id: id | |
, peers: peers | |
, listen: pool.listen | |
, connect: pool.connect | |
}) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment