Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created November 21, 2012 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raynos/4123383 to your computer and use it in GitHub Desktop.
Save Raynos/4123383 to your computer and use it in GitHub Desktop.
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