Skip to content
Browse files

simple relay connections

  • Loading branch information...
1 parent d31b484 commit 81f57ffd71e9a44c34fb8f82ac5c0c88e7a84389 @Raynos committed
Showing with 71 additions and 31 deletions.
  1. +2 −0 browser.js
  2. +51 −0 connections/simpleRelayConnections.js
  3. +16 −30 example/chat/static/index.js
  4. +2 −1 package.json
View
2 browser.js
@@ -6,6 +6,7 @@ var Connection = require("./networks/connection")
, WebRTCNetwork = require("./networks/webRTCNetwork")
, RelayNetwork = require("./networks/relayNetwork")
, RelayConnection = require("./connections/relayConnection")
+ , SimpleRelayConnections = require("./connections/simpleRelayConnections")
, log = require("./log")
module.exports = {
@@ -14,5 +15,6 @@ module.exports = {
, WebRTCNetwork: WebRTCNetwork
, RelayNetwork: RelayNetwork
, RelayConnection: RelayConnection
+ , SimpleRelayConnections: SimpleRelayConnections
, log: log
}
View
51 connections/simpleRelayConnections.js
@@ -0,0 +1,51 @@
+var EventEmitter = require("events").EventEmitter
+ , RelayConnection = require("./relayConnection")
+ , log = require("../log")
+
+module.exports = SimpleRelayConnections
+
+function SimpleRelayConnections(conn) {
+ var relayConnections = {}
+ , rcs = new EventEmitter()
+ , streams = rcs.streams = {}
+
+ rcs.create = create
+ rcs.handleAnswer = handleAnswer
+
+ return rcs
+
+ function create(remotePeerId, offer) {
+ log.info("create", arguments)
+ var rc = relayConnections[remotePeerId] = RelayConnection(conn)
+ if (offer) {
+ rc.on("stream", handleStream)
+
+ rc.receiveOffer(offer)
+
+ return rc.createAnswer(offer)
+ }
+
+ return rc.createOffer()
+
+ function handleStream(stream) {
+ streams[remotePeerId] = stream
+
+ rcs.emit("stream", remotePeerId, stream)
+ }
+ }
+
+ function handleAnswer(remotePeerId, answer) {
+ log.info("handleAnswer", arguments)
+ var rc = relayConnections[remotePeerId]
+
+ rc.on("stream", handleStream)
+
+ rc.receiveAnswer(answer)
+
+ function handleStream(stream) {
+ streams[remotePeerId] = stream
+
+ rcs.emit("stream", remotePeerId, stream)
+ }
+ }
+}
View
46 example/chat/static/index.js
@@ -3,15 +3,15 @@ var DiscoveryNetwork = require("../../../browser")
, Connection = DiscoveryNetwork.Connection
, PeerNetwork = DiscoveryNetwork.PeerNetwork
, RelayNetwork = DiscoveryNetwork.RelayNetwork
- , RelayConnection = DiscoveryNetwork.RelayConnection
+ , SimpleRelayConnections = DiscoveryNetwork.SimpleRelayConnections
+ , forEach = require("iterators").forEachSync
var chatBox = document.getElementById("chat-box")
, chatMessages = document.getElementById("chat-messages")
, chatButton = document.getElementById("chat-button")
// Open discovery connection
, conn = Connection("http://localhost:8081/shoe")
- , connections = {}
- , streams = []
+ , rcs = SimpleRelayConnections(conn)
chatButton.addEventListener("click", sendMessage)
@@ -33,55 +33,41 @@ peerNetwork.on("peer", handlePeer)
relayNetwork.on("offer", handleOffer)
// incoming answers from another peer
-relayNetwork.on("answer", handleAnswer)
+relayNetwork.on("answer", rcs.handleAnswer)
+
+// handle streams coming out of rcs
+rcs.on("stream", handleStream)
peerNetwork.join()
function handlePeer(remotePeerId) {
- var rc = connections[remotePeerId] = RelayConnection(conn)
- , offer = rc.createOffer()
+ var offer = rcs.create(remotePeerId)
relayNetwork.sendOffer(remotePeerId, offer)
}
function handleOffer(remotePeerId, offer) {
- var rc = connections[remotePeerId] = RelayConnection(conn)
-
- rc.on("stream", handleStream)
-
- rc.receiveOffer(offer)
-
- var answer = rc.createAnswer(offer)
+ var answer = rcs.create(remotePeerId, offer)
relayNetwork.sendAnswer(remotePeerId, answer)
}
-function handleAnswer(remotePeerId, answer) {
- var rc = connections[remotePeerId]
-
- rc.on("stream", handleStream)
-
- rc.receiveAnswer(answer)
-}
-
-function handleStream(stream) {
+function handleStream(remotePeerId, stream) {
chatButton.disabled = false
- streams.push(stream)
-
stream.on("data", renderMessage)
-}
-function renderMessage(data) {
- var msg = document.createElement("div")
- msg.textContent = "stranger: " + data
- chatMessages.appendChild(msg)
+ function renderMessage(data) {
+ var msg = document.createElement("div")
+ msg.textContent = "stranger[" + remotePeerId + "]: " + data
+ chatMessages.appendChild(msg)
+ }
}
function sendMessage() {
var text = chatBox.value
chatBox.value = ""
- streams.forEach(send)
+ forEach(rcs.streams, send)
var msg = document.createElement("div")
msg.textContent = "self: " + text
View
3 package.json
@@ -31,7 +31,8 @@
"devDependencies": {
"webrtc-stream": "~0.3.3",
"browserify-server": "~0.3.1",
- "mux-demux-shoe": "~0.2.7"
+ "mux-demux-shoe": "~0.2.7",
+ "iterators": "~0.2.0"
},
"licenses": [
{

0 comments on commit 81f57ff

Please sign in to comment.
Something went wrong with that request. Please try again.