Sfoglia il codice sorgente

ihavenocluewhatimdoing

Michelle Bu 12 anni fa
commit
ad498531cc
4 ha cambiato i file con 169 aggiunte e 0 eliminazioni
  1. 17 0
      package.json
  2. 27 0
      public/js/sink.js
  3. 67 0
      public/js/source.js
  4. 58 0
      server.js

+ 17 - 0
package.json

@@ -0,0 +1,17 @@
+{
+  "name": "p2p",
+  "version": "0.0.0",
+  "description": "Simple p2p file transfer",
+  "main": "server.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1",
+    "start": "node server.js"
+  },
+  "repository": "",
+  "author": "Michelle Bu",
+  "license": "BSD",
+  "dependencies": {
+    "express": "~2.5.11",
+    "socket.io": "~0.9.13"
+  }
+}

+ 27 - 0
public/js/sink.js

@@ -0,0 +1,27 @@
+function SinkPeer(options, readyfn) {
+  this._config = options.config || {};
+  this._name = options.name || 'StreamAPI';
+  this._pc = null;
+  this._id = null;
+  this._dc = null;
+  this._socket = io.connect('http://localhost');
+  this.socketInit(readyfn);
+};
+
+SinkPeer.prototype.socketInit = function(cb) {
+  self = this;
+  this._socket.emit('sink', function(data) {
+    self._id = data.id;
+    self._pc = new mozRTCPeerConnection(self._config);
+    self._socket.on('offer', function(data) {
+      self._pc.setRemoteDescription(data.sdp, function() {
+        self._pc.createAnswer(function(answer) {
+          self._pc.setLocalDescription(answer, function() {
+            self._socket.emit('answer', { 'sdp': answer, 'source': data.source });
+          });
+        });
+      });
+    });
+    cb(self._id);
+  });
+});

+ 67 - 0
public/js/source.js

@@ -0,0 +1,67 @@
+function SourcePeer(options) {
+  this._config = options.config || {};
+  this._streams = options.streamType || 'd';
+  this._name = options.name || 'StreamAPI';
+  // PeerConnections open for this source. Client name => PC.
+  this._pcs = {};
+  this._id = null;
+  // Same for DCs.
+  this._dcs = {};
+  this._socket = io.connect('http://localhost');
+  this.socketInit();
+};
+
+SourcePeer.prototype.socketInit = function() {
+  self = this;
+  this._socket.emit('source', function(data) {
+    self._id = data.id;
+
+    self._socket.on('sink-connected', function(data) {
+      // TODO: not just moz.
+      target = data.sink;
+      var pc = new mozRTCPeerConnection(self._config);
+      self._pcs[target] = pc;
+
+      // Setups will create streams--then the callback sets up the offers.
+      self.handleStream(pc, target, function() {
+        pc.createOffer(function(offer) {
+          pc.setLocalDescription(offer);
+          self._socket.emit('offer', { 'sdp': offer, 'sink': target });
+        });
+      });
+    });
+
+    self._socket.on('answer', function(data) {
+      self._pcs[data.sink].setRemoteDescription(data.sdp);
+    });
+  });
+}
+
+// Based on stream type requested, sets up the stream for PC.
+SourcePeer.prototype.handleStream(pc, target, cb) {
+  if (this._streams === 'v') {
+  } else if (this._streams === 'a') {
+  } else if (this._streams === 'av') {
+  } else if (this._streams === 'd') {
+    this.setupDataChannel(pc, target);
+    var dc = pc.createDataChannel(this._name, {}, target);
+    this._dc[target] = dc;
+    dc.binaryType = 'blob';
+    dc.onmessage = function(e) {
+      // process e.data
+    };
+  } else if (this._streams === 'dav') {
+  } else if (this._streams === 'da') {
+  } else if (this._streams === 'dv') {
+  } else {
+    //error
+  }
+}
+
+SourcePeer.prototype.setupDataChannel = function(pc, target) {
+
+});
+
+SourcePeer.prototype.gotDescription = function(desc) {
+  this._pc
+};

+ 58 - 0
server.js

@@ -0,0 +1,58 @@
+var express = require('express');
+var fs = require('fs');
+var app =  express.createServer();
+var io = require('socket.io').listen(app);
+
+// Initialize main server.
+app.use(express.bodyParser());
+
+app.use(express.static(__dirname + '/public'));
+
+app.set('view engine', 'ejs');
+app.set('views', __dirname + '/views');
+
+// P2P sources: { socket id => url }.
+sources = {};
+// socket.io clients.
+clients = {};
+
+// P2Ps. { source id => group members }
+connections = {};
+
+// For connecting clients:
+// Src will connect upon creating a link.
+// Receivers will connect after clicking a button and entering an optional key.
+io.sockets.on('connection', function(socket) {
+  clients[socket.id] = socket;
+
+  // Source connected.
+  socket.on('source', function(from, msg) {
+
+  });
+
+  // Offer from src to dest.
+  socket.on('offer', function (from, msg) {
+  });
+  // Answer from dest to src.
+  socket.on('answer', function (from, msg) {
+    var source = clients[msg.answer];
+    source.emit('client-connected' { member: msg.member, offer: msg.offer });
+  });
+
+  socket.on('disconnect', function() {
+    // Handle on client side?
+    socket.broadcast.to(connections[socket.id]).emit('Host disconnected');
+    delete connections[socket.id];
+    delete clients[socket.id];
+  });
+});
+
+
+app.get('/', function(req, res){
+  res.render('index');
+});
+
+
+app.listen(80);
+
+