Created
November 13, 2012 08:34
Revisions
-
Raynos revised this gist
Nov 13, 2012 . 1 changed file with 6 additions and 6 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -15,7 +15,7 @@ var uuid = require("node-uuid") , PeerConnection = require("peer-connection") , id = uuid() , peers = Peers("discoverynetwork.co/service" , "unique group name") , connectionHash = {} @@ -36,9 +36,9 @@ peers.on("create", function handlePeer(peer) { stream.write("hello world") }) var self = peers.add({ id: id }) listen(peers, self, function (stream) { // I am an echo stream stream.pipe(stream) }) @@ -53,8 +53,8 @@ function connect(peer) { return pc.connect() } function listen(peers, self, callback) { self.on("offer", extract(function (id, offer) { var pc = connectionHash[id] = PeerConnection() pc.setRemote(offer) @@ -66,7 +66,7 @@ function listen(peers, myself, callback) { }) })) self.on("answer", extract(function (id, answer) { var pc = connectionHash[id] pc.setRemote(answer) -
Raynos revised this gist
Nov 13, 2012 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -38,7 +38,7 @@ peers.on("create", function handlePeer(peer) { peers.add({ id: self }) listen(peers, peers.get(self), function (stream) { // I am an echo stream stream.pipe(stream) }) @@ -53,8 +53,8 @@ function connect(peer) { return pc.connect() } function listen(peers, myself, callback) { myself.on("offer", extract(function (id, offer) { var pc = connectionHash[id] = PeerConnection() pc.setRemote(offer) @@ -66,7 +66,7 @@ function listen(peers, self, callback) { }) })) myself.on("answer", extract(function (id, answer) { var pc = connectionHash[id] pc.setRemote(answer) -
Raynos revised this gist
Nov 13, 2012 . 1 changed file with 9 additions and 11 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -20,16 +20,7 @@ var uuid = require("node-uuid") , "unique group name") , connectionHash = {} peers.on("create", function handlePeer(peer) { if (peer.id < self.id) { // Other peer will open the connection return @@ -43,7 +34,14 @@ function handlePeer(peer) { })) stream.write("hello world") }) peers.add({ id: self }) listen(peers, self, function (stream) { // I am an echo stream stream.pipe(stream) }) function connect(peer) { var pc = connectionHash[peer.id] = PeerConnection() -
Raynos revised this gist
Nov 13, 2012 . 1 changed file with 37 additions and 47 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -9,6 +9,7 @@ var uuid = require("node-uuid") , equal = require("assert").equal , WriteStream = require("write-stream") , extract = require("extract") , Peers = require("peers-signal") @@ -19,68 +20,57 @@ var uuid = require("node-uuid") , "unique group name") , connectionHash = {} peers.on("create", handlePeer) peers.add({ id: self }) listen(peers, self, function (stream) { // I am an echo stream stream.pipe(stream) }) function handlePeer(peer) { if (peer.id < self.id) { // Other peer will open the connection return } var stream = connect(peer) // Connected peer is an echo stream stream.pipe(WriteStream(function (message) { equal(message, "hello world") })) stream.write("hello world") } function connect(peer) { var pc = connectionHash[peer.id] = PeerConnection() pc.createOffer(function (err, offer) { peer.emit("offer", [self.id, offer]) }) return pc.connect() } function listen(peers, self, callback) { self.on("offer", extract(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", extract(function (id, answer) { var pc = connectionHash[id] pc.setRemote(answer) })) } -
Raynos revised this gist
Nov 13, 2012 . 1 changed file with 14 additions and 13 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -25,26 +25,27 @@ peers.on("additions", handlePeer) peers.add({ id: self }) function handlePeer(peer) { var id = peer.id if (id === self) { return peer.on("change", function listen(changes) { if (changes.type === "offer") { handleOffer(changes.value, changes.id) } else if (changes.type === "answer") { handleAnswer(changes.value, changes.id) } }) } var pc = connectionHash[id] = PeerConnection() pc.createOffer(function (err, offer) { peer.emit("change", { type: "offer" , id: self , value: offer }) }) } function handleOffer(offer, id) { -
Raynos revised this gist
Nov 13, 2012 . 1 changed file with 33 additions and 105 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,6 @@ - A signal mechanism to send offers & answers - A simplified peerConnection function */ var uuid = require("node-uuid") @@ -15,81 +14,39 @@ var uuid = require("node-uuid") , PeerConnection = require("peer-connection") , self = uuid() , peers = Peers("discoverynetwork.co/service" , "unique group name") , connectionHash = {} peers.on("additions", handlePeer) peers.add({ id: self }) function handlePeer(peer) { if (peer.id === self) { peer.on("change", function listen(changes) { if (changes.type === "offer") { handleOffer(changes.value, changes.id) } else if (changes.type === "answer") { handleAnswer(changes.value, changes.id) } }) } else { var id = peer.id , pc = connectionHash[id] = PeerConnection() pc.createOffer(function (err, offer) { peer.emit("change", { type: "offer" , id: self , value: offer }) }) } } function handleOffer(offer, id) { var pc = connectionHash[id] = PeerConnection() @@ -102,56 +59,27 @@ function handleOffer(offer, id) { , value: answer }) pc.on("connect", function listenOnChannels() { pc.on("connection", function (stream) { // I am an echo channel stream.pipe(stream) }) }) }) } function handleAnswer(answer, id) { var pc = connectionHash[id] pc.on("connect", function createChannel() { var stream = pc.connect() stream.pipe(WriteStream(function (message) { equal(message, "hello world") })) stream.write("hello world") }) pc.setRemote(answer) } -
Raynos created this gist
Nov 13, 2012 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,157 @@ /* WebRTC consist of a few moving pieces - A signal mechanism for peers - A signal mechanism to send offers & answers - A simplified peerConnection function - A simplified dataChannel function */ var uuid = require("node-uuid") , equal = require("assert").equal , WriteStream = require("write-stream") , Peers = require("peers-signal") , PeerConnection = require("peer-connection") /* Create a token to identify yourself. Open a connection to a peers network. This is hosted at an uri and you can localize the peers you connect to by passing in a unique group name */ , self = uuid() , peers = Peers("discoverynetwork.co/service" , "unique group name") , connectionHash = {} /* For every new peer added handle them */ peers.on("additions", handlePeer) peers.add({ id: self }) /* If the peer is yourself then we listen on events on ourself otherwise we open a peer connection the other peer */ function handlePeer(peer) { peer.id === self ? peer.on("change", listen) : open(peer) } /* Create a fresh peer connection and store it under the id. Then create an offer. An offer is what you send to the peer so that it can find you. Once we have it emit an event on the peer telling him that you have an offer for him */ function open(peer) { var id = peer.id , pc = connectionHash[id] = PeerConnection() pc.createOffer(function (err, offer) { peer.emit("change", { type: "offer" , id: self , value: offer }) }) } /* Listen on either incoming offers or answers */ function listen(changes) { if (changes.type === "offer") { handleOffer(changes.value, changes.id) } else if (changes.type === "answer") { handleAnswer(changes.value, changes.id) } } /* A new offer has arrived. We create a peer connection for the person trying to connect to us. We then set their offer as our remote indentifier, we now know how to reach them We have to create an answer, an answer describes how the remote peer can connect to us. Once we have it we just send back an answer to them and then listen on any new data channels opened on this connection. */ function handleOffer(offer, id) { var pc = connectionHash[id] = PeerConnection() pc.setRemote(offer) pc.createAnswer(function (err, answer) { peers.get(id).emit("change", { type: "answer" , id: self , value: answer }) pc.on("connect", listenOnChannels) }) } /* For incoming answers grab a reference to the peer connection we created for them and set the remote directions to the peer to be the answer. Then we create a data channel to them */ function handleAnswer(answer, id) { var pc = connectionHash[id] pc.on("connect", createChannel) pc.setRemote(answer) } /* We listen on any new channels created on this peer connection. And for each channel we get a stream. The implementation of this connection is to just echo the stream back by piping it into itself. */ function listenOnChannels() { this.on("connection", function (stream) { // I am an echo channel stream.pipe(stream) }) } /* We have an open peer connection so now we can open a data channel. We read incoming data from the stream by piping it into a writer and then write "hello world" to it If this example works correctly our equality assertion holds true. */ function createChannel() { var stream = this.connect() stream.pipe(WriteStream(function (message) { equal(message, "hello world") })) stream.write("hello world") }