Ver código fonte

support passing in constraints

ericz 11 anos atrás
pai
commit
d5badc2cb4
4 arquivos alterados com 54 adições e 51 exclusões
  1. 22 32
      dist/peer.js
  2. 0 0
      dist/peer.min.js
  3. 31 17
      examples/helloworldv.html
  4. 1 2
      lib/negotiator.js

+ 22 - 32
dist/peer.js

@@ -1423,7 +1423,7 @@ Peer.prototype._retrieveId = function(cb) {
 
   // If there's no ID we need to wait for one before trying to init socket.
   http.open('get', url, true);
-  http.onerror = function(e) { 
+  http.onerror = function(e) {
     util.error('Error retrieving ID', e);
     self._abort('server-error', 'Could not get an ID from the server');
   }
@@ -1470,6 +1470,7 @@ Peer.prototype._handleMessage = function(message) {
 
     //
     case 'LEAVE': // Another peer has closed its connection to this peer.
+      util.log('Received leave message from', peer);
       this._cleanupPeer(peer);
       break;
 
@@ -1647,6 +1648,7 @@ Peer.prototype._cleanup = function() {
   for (var i = 0, ii = peers.length; i < ii; i++) {
     this._cleanupPeer(peers[i]);
   }
+
   this.emit('close');
 }
 
@@ -1754,18 +1756,6 @@ DataConnection.prototype._configureDataChannel = function() {
   };
 }
 
-DataConnection.prototype._cleanup = function() {
-  // readyState is deprecated but still exists in older versions.
-  if (this.pc.readyState !== 'closed' || this.pc.signalingState !== 'closed') {
-    this.pc.close();
-    this.open = false;
-    Negotiator.cleanup(this);
-    this.emit('close');
-  } else {
-    this.emit('error', new Error('The connection has already been closed'));
-  }
-}
-
 // Handles a DataChannel message.
 DataConnection.prototype._handleDataMessage = function(e) {
   var self = this;
@@ -1801,7 +1791,9 @@ DataConnection.prototype.close = function() {
   if (!this.open) {
     return;
   }
-  this._cleanup();
+  this.open = false;
+  Negotiator.cleanup(this);
+  this.emit('close');
 }
 
 /** Allows user to send data. */
@@ -1926,11 +1918,12 @@ MediaConnection.prototype.answer = function(stream) {
 
 /** Allows user to close connection. */
 MediaConnection.prototype.close = function() {
-  // TODO: actually close PC.
-  if (this.open) {
-    this.open = false;
-    this.emit('close')
+  if (!this.open) {
+    return;
   }
+  this.open = false;
+  Negotiator.cleanup(this);
+  this.emit('close')
 };
 /**
  * Manages all negotiations between Peers.
@@ -2067,7 +2060,7 @@ Negotiator._setupListeners = function(connection, pc, pc_id) {
       case 'disconnected':
       case 'failed':
         util.log('iceConnectionState is disconnected, closing connections to ' + peerId);
-        Negotiator.cleanup(connection);
+        connection.close();
         break;
       case 'completed':
         pc.onicecandidate = util.noop;
@@ -2110,22 +2103,19 @@ Negotiator._setupListeners = function(connection, pc, pc_id) {
 }
 
 Negotiator.cleanup = function(connection) {
-  connection.close(); // Will fail safely if connection is already closed.
-  util.log('Cleanup PeerConnection for ' + connection.peer);
-  /*if (!!this.pc && (this.pc.readyState !== 'closed' || this.pc.signalingState !== 'closed')) {
-    this.pc.close();
-    this.pc = null;
-  }*/
-
-  connection.provider.socket.send({
-    type: 'LEAVE',
-    dst: connection.peer
-  });
+  util.log('Cleaning up PeerConnection to ' + connection.peer);
+
+  var pc = connection.pc;
+
+  if (!!pc && (pc.readyState !== 'closed' || pc.signalingState !== 'closed')) {
+    pc.close();
+    connection.pc = null;
+  }
 }
 
 Negotiator._makeOffer = function(connection) {
   var pc = connection.pc;
-
+  console.log('using constraints', connection.options.constraints);
   pc.createOffer(function(offer) {
     util.log('Created offer.');
 
@@ -2155,7 +2145,7 @@ Negotiator._makeOffer = function(connection) {
   }, function(err) {
     connection.provider.emit('error', err);
     util.log('Failed to createOffer, ', err);
-  });
+  }, connection.options.constraints);
 }
 
 Negotiator._makeAnswer = function(connection) {

Diferenças do arquivo suprimidas por serem muito extensas
+ 0 - 0
dist/peer.min.js


+ 31 - 17
examples/helloworldv.html

@@ -1,9 +1,9 @@
-<!DOCTYPE HTML> 
-<html lang="en"> 
+<!DOCTYPE HTML>
+<html lang="en">
 <head>
 <title>PeerJS Hello World Code Example</title>
-<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
-<meta http-equiv="Content-Language" content="en-us"> 
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+<meta http-equiv="Content-Language" content="en-us">
 
 <script>
   // Just for demo.
@@ -15,7 +15,7 @@
   };
 </script>
 
-<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> 
+<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
 <script type="text/javascript" src="../dist/peer.js"></script>
 <script>
   // This is a very simple code example. See chat.html for a more involved
@@ -28,27 +28,41 @@
       // see what's going on.
       peer1 = new Peer(window.location.hash.substr(1), { key: 'lwjd5qra8257b9', debug: 3});
       peer1.on('error', console.log);
-      peer1.on('close', console.log);
+      peer1.on('close', function(){
+        console.log('peer closed')
+      });
       peer1.on('call', function(c){
+        window.c = c;
         c.answer(s);
         c.on('stream', function(s){
           window.s = s;
           z = $('<video></video>', {src: URL.createObjectURL(s), autoplay: true}).appendTo('body');
         });
+        c.on('close', function(){
+          console.log('call closed')
+        });
       });
-     
-      
-      
-    
+
+
+
+
     }, function(){});
-    
+
   });
   function call (name) {
-      mc = peer1.call(name, ls);
+      mc = peer1.call(name, ls, {constraints: {
+    mandatory: {
+        OfferToReceiveAudio: false,
+        OfferToReceiveVideo: false
+    }
+}});
       mc.on('stream', function(s){
         window.remote = s;
           z = $('<video></video>', {src: URL.createObjectURL(s), autoplay: true}).appendTo('body');
       });
+      mc.on('close', function(){
+        console.log('my call closed');
+      });
   }
 
 
@@ -81,9 +95,9 @@
     font-weight: 600;
   }
 </style>
-</head> 
- 
-<body> 
+</head>
+
+<body>
   <a href="https://github.com/peers/peerjs"><img style="position: absolute; top: 0; right: 0; border: 0;"
     src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png"
     alt="Fork me on GitHub"></a>
@@ -108,5 +122,5 @@ information see <a href="http://peerjs.com/status">PeerJS WebRTC
   Status</a><br>Note that this demo may also fail if you are behind
 stringent firewalls.</div></div>
 <div class="log" style="color:#FF7500;text-shadow:none;padding:15px;"><strong>Connection status</strong>:<br></div>
-</body> 
-</html> 
+</body>
+</html>

+ 1 - 2
lib/negotiator.js

@@ -188,7 +188,6 @@ Negotiator.cleanup = function(connection) {
 
 Negotiator._makeOffer = function(connection) {
   var pc = connection.pc;
-
   pc.createOffer(function(offer) {
     util.log('Created offer.');
 
@@ -218,7 +217,7 @@ Negotiator._makeOffer = function(connection) {
   }, function(err) {
     connection.provider.emit('error', err);
     util.log('Failed to createOffer, ', err);
-  });
+  }, connection.options.constraints);
 }
 
 Negotiator._makeAnswer = function(connection) {

Alguns arquivos não foram mostrados porque muitos arquivos mudaram nesse diff