Michelle Bu 12 年之前
父节点
当前提交
02507d26a8
共有 3 个文件被更改,包括 27 次插入23 次删除
  1. 1 0
      lib/dataconnection.js
  2. 3 1
      lib/mediaconnection.js
  3. 23 22
      lib/peer.js

+ 1 - 0
lib/dataconnection.js

@@ -24,6 +24,7 @@ function DataConnection(peer, provider, options) {
   this.id = this.options._id || DataConnection._idPrefix + util.randomToken();
 
   this._pc = Negotiator.startConnection(
+    this.type,
     this.peer,
     this.id,
     this.provider,

+ 3 - 1
lib/mediaconnection.js

@@ -18,10 +18,11 @@ function MediaConnection(peer, provider, options) {
   this.id = this.options._id || MediaConnection._idPrefix + util.randomToken();
   if (this.localStream) {
     this._pc = Negotiator.startConnection(
+      this.type,
       this.peer,
       this.id,
       this.provider,
-      {_stream: this.localStream, originator: true  }
+      {_stream: this.localStream, originator: true}
     )
   }
 };
@@ -48,6 +49,7 @@ MediaConnection.prototype.answer = function(stream) {
 
   this.localStream = stream;
   this._pc = Negotiator.startConnection(
+    this.type,
     this.peer,
     this.id,
     this.provider,

+ 23 - 22
lib/peer.js

@@ -14,7 +14,7 @@ function Peer(id, options) {
     id = id.toString();
   }
   //
-  
+
   // Configurize options
   options = util.extend({
     debug: 0, // 1: Errors, 2: Warnings, 3: All logs
@@ -39,7 +39,7 @@ function Peer(id, options) {
   }
   util.setLogLevel(options.debug);
   //
-  
+
   // Sanity checks
   // Ensure WebRTC supported
   if (!util.supports.audioVideo && !util.supports.data ) {
@@ -72,7 +72,23 @@ function Peer(id, options) {
 
   // References
   this.connections = {}; // DataConnections for this peer.
-  this.calls = {}; // MediaConnections for this peer
+  //
+
+  // Initialize the 'socket' (which is actually a mix of XHR streaming and
+  // websockets.)
+  var self = this;
+  this.socket = new Socket(this.options.secure, this.options.host, this.options.port, this.options.key);
+  this.socket.on('message', function(data) {
+    self._handleMessage(data);
+  });
+  this.socket.on('error', function(error) {
+    self._abort('socket-error', error);
+  });
+  this.socket.on('close', function() {
+    if (!self.disconnected) { // If we haven't explicitly disconnected, emit error.
+      self._abort('socket-closed', 'Underlying socket is already closed.');
+    }
+  });
   //
 
   // Start the connections
@@ -119,22 +135,7 @@ Peer.prototype._retrieveId = function(cb) {
 Peer.prototype._initialize = function(id) {
   var self = this;
   this.id = id;
-
-  // Initialize the 'socket' (which is actually a mix of XHR streaming and
-  // websockets.
-  this.socket = new Socket(this.options.secure, this.options.host, this.options.port, this.options.key, this.id);
-  this.socket.on('message', function(data) {
-    self._handleMessage(data);
-  });
-  this.socket.on('error', function(error) {
-    self._abort('socket-error', error);
-  });
-  this.socket.on('close', function() {
-    if (!self.disconnected) { // If we haven't explicitly disconnected, emit error.
-      self._abort('socket-closed', 'Underlying socket is already closed.');
-    }
-  });
-  this.socket.start();
+  this.socket.start(this.id);
 }
 
 /** Handles messages from the server. */
@@ -169,7 +170,7 @@ Peer.prototype._handleMessage = function(message) {
       break;
     case 'OFFER': // we should consider switching this to CALL/CONNECT, but this is the least breaking option.
       var connectionId = payload.connectionId;
-      var connection = this._getConnection(peer, connectionId);
+      var connection = this.getConnection(peer, connectionId);
 
       if (connection) {
         util.warn('Offer received for existing Connection ID:', connectionId);
@@ -202,7 +203,7 @@ Peer.prototype._handleMessage = function(message) {
       break;
     default:
       var id = message.id;
-      var connection = this._getConnection(peer, id);
+      var connection = this.getConnection(peer, id);
 
       if (connection) {
         // Pass it on.
@@ -248,7 +249,7 @@ Peer.prototype._addConnection = function(peer, connection) {
 }
 
 /** Retrieve a data/media connection for this peer. */
-Peer.prototype._getConnection = function(peer, id) {
+Peer.prototype.getConnection = function(peer, id) {
   var connections = this.connections[peer];
   if (!connections) {
     return null;