Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created November 13, 2012 08:34

Revisions

  1. Raynos revised this gist Nov 13, 2012. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions data-channel.js
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ var uuid = require("node-uuid")

    , PeerConnection = require("peer-connection")

    , self = uuid()
    , 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")
    })

    peers.add({ id: self })
    var self = peers.add({ id: id })

    listen(peers, peers.get(self), function (stream) {
    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, myself, callback) {
    myself.on("offer", extract(function (id, offer) {
    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) {
    })
    }))

    myself.on("answer", extract(function (id, answer) {
    self.on("answer", extract(function (id, answer) {
    var pc = connectionHash[id]

    pc.setRemote(answer)
  2. Raynos revised this gist Nov 13, 2012. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions data-channel.js
    Original 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, self, function (stream) {
    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, self, callback) {
    self.on("offer", extract(function (id, offer) {
    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) {
    })
    }))

    self.on("answer", extract(function (id, answer) {
    myself.on("answer", extract(function (id, answer) {
    var pc = connectionHash[id]

    pc.setRemote(answer)
  3. Raynos revised this gist Nov 13, 2012. 1 changed file with 9 additions and 11 deletions.
    20 changes: 9 additions & 11 deletions data-channel.js
    Original 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", handlePeer)

    peers.add({ id: self })

    listen(peers, self, function (stream) {
    // I am an echo stream
    stream.pipe(stream)
    })

    function handlePeer(peer) {
    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()
  4. Raynos revised this gist Nov 13, 2012. 1 changed file with 37 additions and 47 deletions.
    84 changes: 37 additions & 47 deletions data-channel.js
    Original 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("additions", handlePeer)
    peers.on("create", handlePeer)

    peers.add({ id: self })

    listen(peers, self, function (stream) {
    // I am an echo stream
    stream.pipe(stream)
    })

    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)
    }
    })
    if (peer.id < self.id) {
    // Other peer will open the connection
    return
    }

    var pc = connectionHash[id] = PeerConnection()

    pc.createOffer(function (err, offer) {
    peer.emit("change", {
    type: "offer"
    , id: self
    , value: offer
    })
    })
    }
    var stream = connect(peer)

    function handleOffer(offer, id) {
    var pc = connectionHash[id] = PeerConnection()
    // Connected peer is an echo stream
    stream.pipe(WriteStream(function (message) {
    equal(message, "hello world")
    }))

    pc.setRemote(offer)
    stream.write("hello world")
    }

    pc.createAnswer(function (err, answer) {
    peers.get(id).emit("change", {
    type: "answer"
    , id: self
    , value: answer
    })
    function connect(peer) {
    var pc = connectionHash[peer.id] = PeerConnection()

    pc.on("connect", function listenOnChannels() {
    pc.on("connection", function (stream) {
    // I am an echo channel
    stream.pipe(stream)
    })
    })
    pc.createOffer(function (err, offer) {
    peer.emit("offer", [self.id, offer])
    })

    return pc.connect()
    }

    function handleAnswer(answer, id) {
    var pc = connectionHash[id]
    function listen(peers, self, callback) {
    self.on("offer", extract(function (id, offer) {
    var pc = connectionHash[id] = PeerConnection()

    pc.on("connect", function createChannel() {
    var stream = pc.connect()
    pc.setRemote(offer)

    stream.pipe(WriteStream(function (message) {
    equal(message, "hello world")
    }))
    pc.createAnswer(function (err, answer) {
    peers.get(id).emit("answer", [self.id, answer])

    stream.write("hello world")
    })
    pc.on("connection", callback)
    })
    }))

    self.on("answer", extract(function (id, answer) {
    var pc = connectionHash[id]

    pc.setRemote(answer)
    pc.setRemote(answer)
    }))
    }
  5. Raynos revised this gist Nov 13, 2012. 1 changed file with 14 additions and 13 deletions.
    27 changes: 14 additions & 13 deletions data-channel.js
    Original file line number Diff line number Diff line change
    @@ -25,26 +25,27 @@ peers.on("additions", handlePeer)
    peers.add({ id: self })

    function handlePeer(peer) {
    if (peer.id === self) {
    peer.on("change", function listen(changes) {
    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)
    }
    })
    } else {
    var id = peer.id
    , pc = connectionHash[id] = PeerConnection()

    pc.createOffer(function (err, offer) {
    peer.emit("change", {
    type: "offer"
    , id: self
    , value: offer
    })
    })
    }

    var pc = connectionHash[id] = PeerConnection()

    pc.createOffer(function (err, offer) {
    peer.emit("change", {
    type: "offer"
    , id: self
    , value: offer
    })
    })
    }

    function handleOffer(offer, id) {
  6. Raynos revised this gist Nov 13, 2012. 1 changed file with 33 additions and 105 deletions.
    138 changes: 33 additions & 105 deletions data-channel.js
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,6 @@
    - A signal mechanism to send offers & answers
    - A simplified peerConnection function
    - A simplified dataChannel function
    */

    var uuid = require("node-uuid")
    @@ -15,81 +14,39 @@ var uuid = require("node-uuid")

    , 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
    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
    })
    })
    })
    }

    /*
    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()

    @@ -102,56 +59,27 @@ function handleOffer(offer, id) {
    , value: answer
    })

    pc.on("connect", listenOnChannels)
    pc.on("connect", function listenOnChannels() {
    pc.on("connection", function (stream) {
    // I am an echo channel
    stream.pipe(stream)
    })
    })
    })
    }

    /*
    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)
    }
    pc.on("connect", function createChannel() {
    var stream = pc.connect()

    /*
    We listen on any new channels created on this peer
    connection.
    stream.pipe(WriteStream(function (message) {
    equal(message, "hello world")
    }))

    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)
    stream.write("hello world")
    })
    }

    /*
    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")
    }
    pc.setRemote(answer)
    }
  7. Raynos created this gist Nov 13, 2012.
    157 changes: 157 additions & 0 deletions data-channel.js
    Original 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")
    }