Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

updated docs with better example

  • Loading branch information...
commit 1cc06e62459e35bc95f48e9f5942d042bd66b254 1 parent 5f5d9da
@Raynos authored
View
58 README.md
@@ -7,26 +7,56 @@ A peer to peer discovery network in the cloud
See [webrtc-stream for more detailed example][1]
``` js
-var conn = Connection()
+var DiscoveryNetwork = require("discovery-network")
+ , Connection = DiscoveryNetwork.Connection
+ , PeerNetwork = DiscoveryNetwork.PeerNetwork
+ , RelayNetwork = DiscoveryNetwork.RelayNetwork
+ , SimpleRelayConnections = DiscoveryNetwork.SimpleRelayConnections
+ // Open discovery connection
+ , conn = Connection("http://localhost:8081/shoe")
+ , rcs = SimpleRelayConnections(conn)
+
+// Identify ourself with a random UUID
+conn.identify()
+
+var peerNetwork = PeerNetwork(conn, "discovery-network-demo:peer")
+ , relayNetwork = RelayNetwork(conn, "discovery-network-demo:relay")
+
+// when you detect a new peer joining, open a RC to them
+peerNetwork.on("peer", handlePeer)
-// identify yourself to discovery network connection
-conn.identify(userObject)
+// when we detect an offer from the relay network, open an RC to them
+relayNetwork.on("offer", handleOffer)
-// open peer & webrtc networks
-var peerNetwork = PeerNetwork(conn)
- , webrtcNetwork = WebRTCNetwork(conn)
+// incoming answers from another peer
+relayNetwork.on("answer", rcs.handleAnswer)
-// when you detect a new peer joining, open a PC to them
-peerNetwork.on("peer", handlePeer)
+// handle streams coming out of rcs
+rcs.on("stream", handleStream)
-// incoming offer from another peer
-webrtcNetwork.on("offer", handleOffer)
+peerNetwork.join()
-// incoming answers from another peer
-webrtcNetwork.on("answer", handleAnswer)
+function handlePeer(remotePeerId) {
+ var offer = rcs.create(remotePeerId)
+
+ relayNetwork.sendOffer(remotePeerId, offer)
+}
+
+function handleOffer(remotePeerId, offer) {
+ var answer = rcs.create(remotePeerId, offer)
+
+ relayNetwork.sendAnswer(remotePeerId, answer)
+}
+
+function handleStream(remotePeerId, stream) {
+ stream.write("hello!")
+
+ stream.on("data", log)
-// incoming candidates from another peer
-webrtcNetwork.on("candidate", handleCandidate)
+ function log(data) {
+ console.log("data from peer", remotePeerId, data)
+ }
+}
```
## Installation
View
3  discovery-network-server/.npmignore
@@ -0,0 +1,3 @@
+node_modules
+*.log
+*.err
View
4 discovery-network-server/.travis.yml
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+ - 0.6
+ - 0.8
View
19 discovery-network-server/LICENCE
@@ -0,0 +1,19 @@
+Copyright (c) 2012 Raynos.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
View
10 discovery-network-server/Makefile
@@ -0,0 +1,10 @@
+node-test:
+ ./node_modules/.bin/tap --stderr ./test
+
+test:
+ ./node_modules/.bin/testem \
+ --file testem.json \
+ --debuglog testem.log \
+ --debug 2> testem.err
+
+.PHONY: test
View
22 discovery-network-server/README.md
@@ -0,0 +1,22 @@
+# discovery-network-server [![build status][1]][2]
+
+The server for the discoverynetwork
+
+## Example
+
+## Installation
+
+`npm install discovery-network-server`
+
+## Tests
+
+`make test`
+
+## Contributors
+
+ - Raynos
+
+## MIT Licenced
+
+ [1]: https://secure.travis-ci.org/Raynos/discovery-network-server.png
+ [2]: http://travis-ci.org/Raynos/discovery-network-server
View
0  discovery-network-server/index.js
No changes.
View
36 discovery-network-server/package.json
@@ -0,0 +1,36 @@
+{
+ "name": "discovery-network-server",
+ "version": "0.0.1",
+ "description": "The server for the discoverynetwork",
+ "keywords": [],
+ "author": "Raynos <raynos2@gmail.com>",
+ "repository": "git://github.com/Raynos/discovery-network-server.git",
+ "main": "index",
+ "homepage": "https://github.com/Raynos/discovery-network-server",
+ "contributors": [
+ {
+ "name": "Jake Verbaten"
+ }
+ ],
+ "bugs": {
+ "url": "https://github.com/Raynos/discovery-network-server/issues",
+ "email": "raynos2@gmail.com"
+ },
+ "dependencies": {
+ },
+ "devDependencies": {
+ "testling": "git://github.com/Raynos/testling#master",
+ "testem": "git://github.com/Raynos/testem#master",
+ "sinon": "git://github.com/Raynos/Sinon.JS.git",
+ "tap": "0.2.5"
+ },
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "http://github.com/Raynos/discovery-network-server/raw/master/LICENSE"
+ }
+ ],
+ "scripts": {
+ "test": "make node-test"
+ }
+}
View
14 discovery-network-server/testem.json
@@ -0,0 +1,14 @@
+{
+ "framework": "custom"
+ , "browserify": true
+ , "src_files": [
+ "test/**/*.js"
+ , "node_modules/testem/adapters/testling.js"
+ ]
+ , "watch_files": [
+ "test/**/*.js"
+ , "./index.js"
+ ]
+ , "node": "node_modules/.bin/tap"
+ , "node-args": ["--tap", "test"]
+}
View
4 example/chat/static/index.js
@@ -23,8 +23,8 @@ discoveryLog.enabled = true
// Identify ourself with a random UUID
conn.identify()
-var peerNetwork = PeerNetwork(conn)
- , relayNetwork = RelayNetwork(conn)
+var peerNetwork = PeerNetwork(conn, "discovery-network-demo:peer")
+ , relayNetwork = RelayNetwork(conn, "discovery-network-demo:relay")
// when you detect a new peer joining, open a RC to them
peerNetwork.on("peer", handlePeer)
View
5 example/simple/server.js
@@ -0,0 +1,5 @@
+var browserifyServer = require("browserify-server")
+
+var server = browserifyServer.listen(__dirname, 8080)
+
+console.log("running on port 8080")
View
10 example/simple/static/index.html
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title> simple demo </title>
+ </head>
+ <body>
+
+ <script src="bundle.js"></script>
+ </body>
+</html>
View
50 example/simple/static/index.js
@@ -0,0 +1,50 @@
+var DiscoveryNetwork = require("../../../browser")
+ , Connection = DiscoveryNetwork.Connection
+ , PeerNetwork = DiscoveryNetwork.PeerNetwork
+ , RelayNetwork = DiscoveryNetwork.RelayNetwork
+ , SimpleRelayConnections = DiscoveryNetwork.SimpleRelayConnections
+ // Open discovery connection
+ , conn = Connection("http://localhost:8081/shoe")
+ , rcs = SimpleRelayConnections(conn)
+
+// Identify ourself with a random UUID
+conn.identify()
+
+var peerNetwork = PeerNetwork(conn, "discovery-network-demo:peer")
+ , relayNetwork = RelayNetwork(conn, "discovery-network-demo:relay")
+
+// when you detect a new peer joining, open a RC to them
+peerNetwork.on("peer", handlePeer)
+
+// when we detect an offer from the relay network, open an RC to them
+relayNetwork.on("offer", handleOffer)
+
+// incoming answers from another peer
+relayNetwork.on("answer", rcs.handleAnswer)
+
+// handle streams coming out of rcs
+rcs.on("stream", handleStream)
+
+peerNetwork.join()
+
+function handlePeer(remotePeerId) {
+ var offer = rcs.create(remotePeerId)
+
+ relayNetwork.sendOffer(remotePeerId, offer)
+}
+
+function handleOffer(remotePeerId, offer) {
+ var answer = rcs.create(remotePeerId, offer)
+
+ relayNetwork.sendAnswer(remotePeerId, answer)
+}
+
+function handleStream(remotePeerId, stream) {
+ stream.write("hello!")
+
+ stream.on("data", log)
+
+ function log(data) {
+ console.log("data from peer", remotePeerId, data)
+ }
+}
View
4 example/webrtc/static/index.js
@@ -22,8 +22,8 @@ MediaStream.local(localVideo, function (myMediaStream) {
conn.identify()
- var peerNetwork = PeerNetwork(conn)
- , webrtcNetwork = WebRTCNetwork(conn)
+ var peerNetwork = PeerNetwork(conn, "discovery-network-demo:peer")
+ , webrtcNetwork = WebRTCNetwork(conn, "discovery-network-demo:webrtc")
// when you detect a new peer joining, open a PC to them
peerNetwork.on("peer", handlePeer)
View
6 index.js
@@ -17,9 +17,9 @@ function DiscoveryNetwork(options) {
, prefix = options.prefix || "/discovery"
, router = StreamRouter()
- router.addRoute(prefix + "/peer/echo", EchoChamber())
- router.addRoute(prefix + "/webrtc/echo", EchoChamber())
- router.addRoute(prefix + "/relay/echo", EchoChamber())
+ router.addRoute(prefix + "/peer/echo/:streamName", EchoChamber())
+ router.addRoute(prefix + "/webrtc/echo/:streamName", EchoChamber())
+ router.addRoute(prefix + "/relay/echo/:streamName", EchoChamber())
router.addRoute(prefix + "/relay/offer", handleOffer)
router.addRoute(prefix + "/relay/answer", handleAnswer)
View
4 networks/peerNetwork.js
@@ -4,11 +4,11 @@ var RemoteEventEmitter = require("remote-events")
module.exports = PeerNetwork
-function PeerNetwork(connection) {
+function PeerNetwork(connection, channel) {
var mx = connection.mx
, networkName = connection.networkName
, localPeerId = connection.selfId
- , peerStream = mx.createStream(networkName + "/peer/echo")
+ , peerStream = mx.createStream(networkName + "/peer/echo/" + channel)
, peerEmitter = new RemoteEventEmitter()
, network = new EventEmitter()
View
4 networks/relayNetwork.js
@@ -4,11 +4,11 @@ var RemoteEventEmitter = require("remote-events")
module.exports = RelayNetwork
-function RelayNetwork(connection) {
+function RelayNetwork(connection, channel) {
var mx = connection.mx
, networkName = connection.networkName
, localPeerId = connection.selfId
- , relayStream = mx.createStream(networkName + "/relay/echo")
+ , relayStream = mx.createStream(networkName + "/relay/echo/" + channel)
, relayEmitter = new RemoteEventEmitter()
, network = new EventEmitter()
View
5 networks/webRTCNetwork.js
@@ -6,11 +6,12 @@ var RemoteEventEmitter = require("remote-events")
module.exports = WebRTCNetwork
-function WebRTCNetwork(connection) {
+function WebRTCNetwork(connection, channel) {
var mx = connection.mx
, networkName = connection.networkName
, localPeerId = connection.selfId
- , webrtcStream = mx.createStream(networkName + "/webrtc/echo")
+ , webrtcStream = mx.createStream(
+ networkName + "/webrtc/echo/" + channel)
, webrtcEmitter = new RemoteEventEmitter()
, network = new EventEmitter()
View
2  relays/answer.js
@@ -12,6 +12,8 @@ function handleAnswer(answerStream) {
return answerStream.error("invalid answer token")
}
+ store.delete(token)
+
offerStream.write("open")
answerStream.write("open")
answerStream.pipe(offerStream).pipe(answerStream)
Please sign in to comment.
Something went wrong with that request. Please try again.