Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

example

  • Loading branch information...
commit f21af4e8713be08c09f9db99737aba864bf23153 1 parent 9f0cac8
@Raynos authored
View
10 example/discovery.js
@@ -0,0 +1,10 @@
+var http = require("http")
+ , shoe = require("mux-demux-shoe")
+ , DiscoveryNetwork = require("..")
+
+var server = http.createServer().listen(8081)
+ , network = DiscoveryNetwork()
+
+shoe(network).install(server, "/shoe")
+
+console.log("running on port 8081")
View
10 example/webrtc/server.js
@@ -0,0 +1,10 @@
+var browserifyServer = require("browserify-server")
+ , shoe = require("mux-demux-shoe")
+ , DiscoveryNetwork = require("../..")
+
+var server = browserifyServer.listen(__dirname, 8080)
+ , network = DiscoveryNetwork()
+
+shoe(network).install(server, "/shoe")
+
+console.log("running on port 8080")
View
12 example/webrtc/static/index.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title> WebRTC stream </title>
+ </head>
+ <body>
+ <video id="local-webrtc" autoplay></video>
+ <div id="remote-videos"></div>
+
+ <script src="bundle.js"></script>
+ </body>
+</html>
View
73 example/webrtc/static/index.js
@@ -0,0 +1,73 @@
+var WebRTC = require("webrtc-stream")
+ , MediaStream = WebRTC.MediaStream
+ , SimplePeerConnections = WebRTC.SimplePeerConnections
+ , webrtcLog = WebRTC.log
+ , DiscoveryNetwork = require("../../../browser")
+ , discoveryLog = DiscoveryNetwork.log
+ , Connection = DiscoveryNetwork.Connection
+ , PeerNetwork = DiscoveryNetwork.PeerNetwork
+ , WebRTCNetwork = DiscoveryNetwork.WebRTCNetwork
+
+var localVideo = document.getElementById("local-webrtc")
+ , remoteVideos = document.getElementById("remote-videos")
+
+MediaStream.local(localVideo, function (myMediaStream) {
+ var conn = Connection("http://localhost:8081/shoe")
+ , pcs = SimplePeerConnections(myMediaStream)
+
+ conn.on("connect", function () {
+ console.log("open")
+ })
+
+ webrtcLog.enabled = true
+ discoveryLog.enabled = true
+
+ webrtcLog.info("networkConnection", conn, pcs)
+
+ conn.identify()
+
+ var peerNetwork = PeerNetwork(conn)
+ , webrtcNetwork = WebRTCNetwork(conn)
+
+ // when you detect a new peer joining, open a PC to them
+ peerNetwork.on("peer", handlePeer)
+
+ // incoming offer from another peer
+ webrtcNetwork.on("offer", handleOffer)
+
+ // incoming answers from another peer
+ webrtcNetwork.on("answer", pcs.handleAnswer)
+
+ // incoming candidates from another peer
+ webrtcNetwork.on("candidate", pcs.handleCandidate)
+
+ // outgoing candidates to another peer
+ pcs.on("candidate", webrtcNetwork.sendCandidate)
+
+ // render streams from pcs
+ pcs.on("stream", renderStream)
+
+ peerNetwork.join()
+
+ function handlePeer(remotePeerId) {
+ webrtcLog.info("handlePeer", remotePeerId)
+ var offer = pcs.create(remotePeerId)
+
+ webrtcNetwork.sendOffer(remotePeerId, offer)
+ }
+
+ function handleOffer(remotePeerId, offer) {
+ webrtcLog.info("handleOffer", arguments)
+ var answer = pcs.create(remotePeerId, offer)
+
+ webrtcNetwork.sendAnswer(remotePeerId, answer)
+ }
+
+ function renderStream(remotePeerId, stream) {
+ webrtcLog.info("onRemoteStream", stream)
+ var remoteVideo = document.createElement("video")
+ remoteVideo.autoplay = true
+ remoteVideos.appendChild(remoteVideo)
+ MediaStream.remote(remoteVideo, stream)
+ }
+})
View
3  lib/connection.js
@@ -5,9 +5,10 @@ var shoe = require("shoe")
module.exports = Connection
-function Connection(networkName, uri) {
+function Connection(uri, networkName) {
networkName = networkName || "/discovery"
+ console.log("opening shoe on ", uri)
var stream = shoe(uri || "/shoe")
, mx = MuxDemux()
, conn = new EventEmitter()
View
6 package.json
@@ -27,7 +27,11 @@
"remote-events": "~1.1.1",
"browser-log": "0.0.2"
},
- "devDependencies": {},
+ "devDependencies": {
+ "webrtc-stream": "~0.3.3",
+ "browserify-server": "~0.3.1",
+ "mux-demux-shoe": "~0.2.7"
+ },
"licenses": [
{
"type": "MIT",
Please sign in to comment.
Something went wrong with that request. Please try again.