Michelle Bu %!s(int64=12) %!d(string=hai) anos
pai
achega
ec3cefa6d2
Modificáronse 3 ficheiros con 49 adicións e 32 borrados
  1. 30 15
      lib/dataconnection.js
  2. 14 16
      lib/peer.js
  3. 5 1
      lib/util.js

+ 30 - 15
lib/dataconnection.js

@@ -1,7 +1,7 @@
 /**
  * Wraps a DataChannel between two Peers.
  */
-function DataConnection(peer, options) {
+function DataConnection(peer, provider, options) {
   if (!(this instanceof DataConnection)) return new DataConnection(peer, options);
   EventEmitter.call(this);
 
@@ -12,30 +12,45 @@ function DataConnection(peer, options) {
 
   // Connection is not open yet.
   this.open = false;
-
-  this.label = options.label;
-  this.metadata = options.metadata;
-  this.serialization = options.serialization;
+  this.type = 'data';
   this.peer = peer;
-  this.reliable = options.reliable;
-
-  this.id = options.id; // TODO: or randomly generated ID.
-  if (options.sdp) {
-    // TODO: startConnection ideally calls handleSDP with the offer.
-    // On this side though, do we also need to pass in this peer's ID? Probably.
-    // Maybe instead of having a global negotiator, have a negotiator instance
-    // per Peer?
-    Negotiator.startConnection(this.peer, this.id, options.sdp, options.config);
+  this.provider = provider;
+
+  if (options) {
+    this.label = options.label;
+    this.metadata = options.metadata;
+    this.serialization = options.serialization;
+    this.reliable = options.reliable;
+
+    this.id = options._id;
   }
 
+  if (!this.id) {
+    this.id = DataConnection._idPrefix + util.randomToken();
+  }
+
+  this._pc = Negotiator.startConnection(
+    this.peer,
+    this.id,
+    this.provider,
+    (options && options.payload) ? options.payload : null
+  );
+
   /*this._dc = dc;
   if (this._dc) {
     this._configureDataChannel();
   }*/
-};
+}
 
 util.inherits(DataConnection, EventEmitter);
 
+DataConnection._idPrefix = 'dc_';
+
+/** Called by the Negotiator when the DataChannel is ready. */
+DataConnection.prototype.initialize = function(dc) {
+  this._dc = dc;
+}
+
 DataConnection.prototype._configureDataChannel = function() {
   var self = this;
   // TODO: util.supports.binary

+ 14 - 16
lib/peer.js

@@ -156,6 +156,7 @@ Peer.prototype._handleMessage = function(message) {
       this._abort('invalid-key', 'API KEY "' + this._key + '" is invalid');
       break;
 
+    //
     case 'LEAVE': // Another peer has closed its connection to this peer.
       this._cleanupPeer(peer);
       break;
@@ -165,29 +166,25 @@ Peer.prototype._handleMessage = function(message) {
       this.emit('error', new Error('Could not connect to peer ' + peer));
       break;
     case 'OFFER': // we should consider switching this to CALL/CONNECT, but this is the least breaking option.
-      var id = message.id;
-      var connection = this._getConnection(peer, id);
+      var connectionId = payload.connectionId;
+      var connection = this._getConnection(peer, connectionId);
 
       if (connection) {
-        // Pass it on
-        // TODO: is this just a no-op for DataConnection then? I am under the
-        // impression that only MediaConnection need to renegotiate. I'm not
-        // even sure how a DataConnection would handle renegotiation.
-        connection.handleMessage(message);
+        util.warn('Offer received for existing Connection ID:', connectionId);
+        //connection.handleMessage(message);
       } else {
         // Create a new connection.
         if (payload.type === 'call') {
-          var call = new MediaConnection(peer, {
-            id: id,
-            offer: payload.sdp
+          var call = new MediaConnection(peer, this, {
+            _id: connectionId,
+            _payload: payload // A regular *Connection would have no payload.
           });
           this._addConnection(peer, call);
           this.emit('call', call);
         } else if (payload.type === 'connect') {
-          var connection = new DataConnection(peer, {
-            id: id,
-            offer: payload.sdp,
-            config: this.options.config
+          var connection = new DataConnection(peer, this, {
+            _id: connectionId,
+            _payload: payload
           });
           this._addConnection(peer, connection);
           this.emit('connection', connection);
@@ -201,6 +198,7 @@ Peer.prototype._handleMessage = function(message) {
       var connection = this._getConnection(peer, id);
 
       if (connection) {
+        // Pass it on.
         connection.handleMessage(message);
       } else {
         util.warn('You aborted your connection to ' + peer + ' before it opened.');
@@ -214,7 +212,7 @@ Peer.prototype._handleMessage = function(message) {
  * complete list of options.
  */
 Peer.prototype.connect = function(peer, options) {
-  var connection = new DataConnection(peer, options);
+  var connection = new DataConnection(peer, this, options);
   this._addConnection(peer, connection);
   return connection;
 }
@@ -229,7 +227,7 @@ Peer.prototype.call = function(peer, stream, options) {
     return;
   }
   options.stream = stream;
-  var call = new MediaConnection(peer, options);
+  var call = new MediaConnection(peer, this, options);
   this._addConnection(peer, call);
   return call;
 }

+ 5 - 1
lib/util.js

@@ -23,7 +23,11 @@ var util = {
     }
   },
   setLogFunction: function(fn) {
-    util._print = fn;
+    if (fn.constructor !== Function) {
+      util.warn('The log function you passed in is not a function. Defaulting to regular logs.');
+    } else {
+      util._print = fn;
+    }
   },
   _printWith: function(prefix) {
     return function() {}