Browse Source

connectionmanager

Michelle Bu 12 years ago
parent
commit
928965445a
2 changed files with 17 additions and 69 deletions
  1. 2 64
      lib/connection.js
  2. 15 5
      lib/peer.js

+ 2 - 64
lib/connection.js

@@ -1,5 +1,5 @@
 /**
- * A DataChannel|PeerConnection between two Peers.
+ * Wraps a DataChannel between two Peers.
  */
 function DataConnection(id, peer, socket, options) {
   if (!(this instanceof DataConnection)) return new DataConnection(options);
@@ -38,10 +38,6 @@ DataConnection.prototype.initialize = function(id, socket) {
   if (!!socket) {
     this._socket = socket;
   }
-  // Firefoxism: connectDataConnection ports.
-  /*if (util.browserisms === 'Firefox') {
-    this._firefoxPortSetup();
-  }*/
 
   // Set up PeerConnection.
   this._startPeerConnection();
@@ -63,11 +59,6 @@ DataConnection.prototype.initialize = function(id, socket) {
     this.handleSDP(this._sdp, 'OFFER');
   }
 
-  // Makes offer if Firefox
-  /*if (util.browserisms === 'Firefox') {
-    this._firefoxAdditional();
-  }*/
-
   // No-op this.
   this.initialize = function() {};
 };
@@ -134,24 +125,6 @@ DataConnection.prototype._setupIce = function() {
   };
 };
 
-
-/*DataConnection.prototype._firefoxPortSetup = function() {
-  if (!DataConnection.usedPorts) {
-    DataConnection.usedPorts = [];
-  }
-  this.localPort = util.randomPort();
-  while (DataConnection.usedPorts.indexOf(this.localPort) != -1) {
-    this.localPort = util.randomPort();
-  }
-  this.remotePort = util.randomPort();
-  while (this.remotePort === this.localPort ||
-      DataConnection.usedPorts.indexOf(this.localPort) != -1) {
-    this.remotePort = util.randomPort();
-  }
-  DataConnection.usedPorts.push(this.remotePort);
-  DataConnection.usedPorts.push(this.localPort);
-}*/
-
 DataConnection.prototype._configureDataChannel = function() {
   var self = this;
   
@@ -178,18 +151,6 @@ DataConnection.prototype._configureDataChannel = function() {
   };
 };
 
-
-/** Decide whether to handle Firefoxisms. */
-/*DataConnection.prototype._firefoxAdditional = function() {
-  var self = this;
-  getUserMedia({ audio: true, fake: true }, function(s) {
-    self._pc.addStream(s);
-    if (self._originator) {
-      self._makeOffer();
-    }
-  }, function(err) { util.log('Could not getUserMedia'); });
-};*/
-
 DataConnection.prototype._makeOffer = function() {
   var self = this;
   this._pc.createOffer(function(offer) {
@@ -328,18 +289,7 @@ DataConnection.prototype.handleSDP = function(sdp, type) {
   var self = this;
   this._pc.setRemoteDescription(sdp, function() {
     util.log('Set remoteDescription: ' + type);
-    // Firefoxism
-    /**if (type === 'ANSWER' && util.browserisms === 'Firefox') {
-      self._pc.connectDataConnection(self.localPort, self.remotePort);
-      self._socket.send({
-        type: 'PORT',
-        dst: self.peer,
-        payload: {
-          remote: self.localPort,
-          local: self.remotePort
-        }
-      });
-    } else*/ if (type === 'OFFER') {
+    if (type === 'OFFER') {
       self._makeAnswer();
     }
   }, function(err) {
@@ -360,15 +310,3 @@ DataConnection.prototype.handleLeave = function() {
   util.log('Peer ' + this.peer + ' disconnected');
   this.close();
 };
-
-/*
-DataConnection.prototype.handlePort = function(message) {
-  if (!DataConnection.usedPorts) {
-    DataConnection.usedPorts = [];
-  }
-  DataConnection.usedPorts.push(message.local);
-  DataConnection.usedPorts.push(message.remote);
-  this._pc.connectDataConnection(message.local, message.remote);
-};
-*/
-

+ 15 - 5
lib/peer.js

@@ -41,6 +41,8 @@ function Peer(id, options) {
 
   // Connections for this peer.
   this.connections = {};
+  // Connection managers.
+  this.managers = {};
 
   // Queued connections to make.
   this._queued = [];
@@ -215,15 +217,23 @@ Peer.prototype.connect = function(peer, options) {
   }
 
   options = util.extend({
-    config: this._options.config
+    config: this._options.config,
+    label: 'peerjs'
   }, options);
 
-  var connection = new DataConnection(this.id, peer, this._socket, options);
-  this._attachConnectionListeners(connection);
+  var manager = this.managers[peer];
+  if (!!manager) {
+    manager = new ConnectionManager(this.id, peer, this._socket, options);
+    this._attachManagerListeners(manager);
+    this.managers[peer] = manager;
+    this.connections[peer] = [];
+  }
+
+  var connection = manager.connect(options.label);
+  this.connections[peer].push(connection);
 
-  this.connections[peer] = connection;
   if (!this.id) {
-    this._queued.push(connection);
+    this._queued.push(manager);
   }
   return connection;
 };