Skip to content

Instantly share code, notes, and snippets.

@dominictarr
Forked from Raynos/data-channel.js
Created November 13, 2012 19:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dominictarr/4067887 to your computer and use it in GitHub Desktop.
Save dominictarr/4067887 to your computer and use it in GitHub Desktop.
/* WebRTC consist of a few moving pieces
- A signal mechanism for peers
- A signal mechanism to send offers & answers
- A simplified peerConnection function
*/
var uuid = require("node-uuid")
, assert = require("assert")
, WriteStream = require("write-stream")
, Peers = require("peers-signal")
, PeerConnection = require("peer-connection")
, id = uuid()
, peers = Peers("discoverynetwork.co/service"
, "unique group name")
, connectionHash = {}
peers.on("create", function handlePeer(peer) {
if (peer.id < self.id) {
// Other peer will open the connection
return
}
//create an offer
var pc = PeerConnection()
.createOffer(function (err, offer) {
//surely, createOffer can emit this event?
//why do this manually?
peer.emit("offer", [self.id, offer])
})
//the stream will be buffered untill the handshake is complete.
//emits 'connect' when the handshake is complete.
//emits 'error' is handshake fails/times out.
var stream = pc.connect()
//assert we get the same message back.
stream.pipe(WriteStream(function (message) {
assert.equal(message, "hello world")
}))
stream.write("hello world")
})
var self = peers.add({ id: id })
self.on("offer", function (id, offer) {
var pc = connectionHash[id] = PeerConnection()
pc.setRemote(offer)
pc.createAnswer(function (err, answer) {
peers.get(id).emit("answer", [self.id, answer])
pc.on("connection", callback)
})
})
self.on("answer", function (id, answer) {
var pc = connectionHash[id]
pc.setRemote(answer)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment