-
-
Save dominictarr/4067887 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* 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