Michelle Bu 12 жил өмнө
parent
commit
942833c64e
4 өөрчлөгдсөн 28 нэмэгдсэн , 32 устгасан
  1. 1 0
      changelog.md
  2. 2 16
      lib/dataconnection.js
  3. 16 8
      lib/peer.js
  4. 9 8
      lib/socket.js

+ 1 - 0
changelog.md

@@ -6,6 +6,7 @@
   instead have a randomly-generated ID.
 * Add `util.supports.[FEATURE]` flags, which represent the WebRTC features
   supported by your browser.
+* Additional logging levels (warnings, errors, all).
 
 ## Version 0.2.8 (1 July 2013)
 * Fix bug, no error on Firefox 24 due to missing error callback.

+ 2 - 16
lib/dataconnection.js

@@ -19,7 +19,7 @@ function DataConnection(peer, dc, options) {
   this.reliable = options.reliable;
 
   this._dc = dc;
-  if (!!this._dc) {
+  if (this._dc) {
     this._configureDataChannel();
   }
 };
@@ -60,7 +60,7 @@ DataConnection.prototype._configureDataChannel = function() {
 };
 
 DataConnection.prototype._cleanup = function() {
-  if (!!this._dc && this._dc.readyState !== 'closed') {
+  if (this._dc && this._dc.readyState !== 'closed') {
     this._dc.close();
     this._dc = null;
   }
@@ -146,20 +146,6 @@ DataConnection.prototype.handleMessage = function(message) {
   var payload = message.payload;
 
   switch (message.type) {
-    case 'OFFER':
-      var options = {
-        sdp: payload.sdp,
-        labels: payload.labels,
-        config: this.options.config
-      };
-      // Either forward to or create new manager
-
-      if (!manager) {
-        manager = this._createManager(managerId, peer, options);
-      }
-      manager.handleUpdate(options.labels);
-      manager.handleSDP(payload.sdp, message.type, payload.call);
-      break;
     case 'EXPIRE':
       peer.emit('error', new Error('Could not connect to peer ' + manager.peer));
       break;

+ 16 - 8
lib/peer.js

@@ -9,7 +9,7 @@ function Peer(id, options) {
   if (id && id.constructor == Object) {
     options = id;
     id = undefined;
-  } else {
+  } else if (id) {
     // Ensure id is a string
     id = id.toString();
   }
@@ -30,7 +30,7 @@ function Peer(id, options) {
   }
   // Set whether we use SSL to same as current host
   if (options.secure === undefined) {
-    options.secure = util.isSecure();  
+    options.secure = util.isSecure();
   }
   // TODO: document this feature
   // Set a custom log function if present
@@ -85,13 +85,16 @@ function Peer(id, options) {
 
 util.inherits(Peer, EventEmitter);
 
+/** Get a unique ID from the server via XHR. */
 Peer.prototype._retrieveId = function(cb) {
   var self = this;
   var http = new XMLHttpRequest();
+  // TODO: apparently using ://something.com gives relative protocol?
   var protocol = this.options.secure ? 'https://' : 'http://';
   var url = protocol + this.options.host + ':' + this.options.port + '/' + this.options.key + '/id';
   var queryString = '?ts=' + new Date().getTime() + '' + Math.random();
   url += queryString;
+
   // 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) { 
@@ -111,20 +114,24 @@ Peer.prototype._retrieveId = function(cb) {
   http.send(null);
 };
 
-
+/** Initialize a connection with the server. */
 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('server-message', function(data) {
+  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() {
-    // TODO: What if we disconnected on purpose?
-    self._abort('socket-closed', 'Underlying socket has closed');
+    if (!self.disconnected) { // If we haven't explicitly disconnected, emit error.
+      self._abort('socket-closed', 'Underlying socket has closed');
+    }
   });
   this.socket.start();
 }
@@ -169,7 +176,8 @@ Peer.prototype._handleMessage = function(message) {
           var connection = new DataConnection(peer, {
             id: id,
             offer: offer,
-            sdp: payload.sdp
+            sdp: payload.sdp,
+            config: this.options.config
           });
           this._addConnection(peer, connection);
           this.emit('connection', connection);
@@ -282,11 +290,11 @@ Peer.prototype.disconnect = function() {
   var self = this;
   util.setZeroTimeout(function(){
     if (!self.disconnected) {
+      self.disconnected = true;
       if (self.socket) {
         self.socket.close();
       }
       self.id = null;
-      self.disconnected = true;
     }
   });
 }

+ 9 - 8
lib/socket.js

@@ -11,6 +11,7 @@ function Socket(secure, host, port, key, id) {
 
   this.disconnected = false;
 
+  // TODO: same secure thing as the comment in peer.js
   var protocol = secure ? 'https://' : 'http://';
   var wsProtocol = secure ? 'wss://' : 'ws://';
   this._httpUrl = protocol + host + ':' + port + '/' + key + '/' + id + '/' + token;
@@ -31,7 +32,7 @@ Socket.prototype.start = function() {
 Socket.prototype._startWebSocket = function() {
   var self = this;
 
-  if (!!this._socket) {
+  if (this._socket) {
     return;
   }
 
@@ -51,7 +52,7 @@ Socket.prototype._startWebSocket = function() {
   // Take care of the queue of connections if necessary and make sure Peer knows
   // socket is open.
   this._socket.onopen = function() {
-    if (!!self._timeout) {
+    if (self._timeout) {
       clearTimeout(self._timeout);
       setTimeout(function(){
         self._http.abort();
@@ -71,11 +72,11 @@ Socket.prototype._startXhrStream = function(n) {
     this._http._streamIndex = n || 0;
     this._http.open('post', this._httpUrl + '/id?i=' + this._http._streamIndex, true);
     this._http.onreadystatechange = function() {
-      if (this.readyState == 2 && !!this.old) {
+      if (this.readyState == 2 && this.old) {
         this.old.abort();
         delete this.old;
       }
-      if (this.readyState > 2 && this.status == 200 && !!this.responseText) {
+      if (this.readyState > 2 && this.status == 200 && this.responseText) {
         self._handleStream(this);
       }
     };
@@ -93,7 +94,7 @@ Socket.prototype._handleStream = function(http) {
   var messages = http.responseText.split('\n');
 
   // Check to see if anything needs to be processed on buffer.
-  if (!!http._buffer) {
+  if (http._buffer) {
     while (http._buffer.length > 0) {
       var index = http._buffer.shift();
       var bufferedMessage = messages[index];
@@ -108,7 +109,7 @@ Socket.prototype._handleStream = function(http) {
   }
 
   var message = messages[http._index];
-  if (!!message) {
+  if (message) {
     http._index += 1;
     // Buffering--this message is incomplete and we'll get to it next time.
     // This checks if the httpResponse ended in a `\n`, in which case the last
@@ -143,9 +144,9 @@ Socket.prototype._setHTTPTimeout = function() {
   }, 25000);
 };
 
-
+/** Is the websocket currently open? */
 Socket.prototype._wsOpen = function() {
-  return !!this._socket && this._socket.readyState == 1;
+  return this._socket && this._socket.readyState == 1;
 };
 
 /** Exposed send for DC & Peer. */