浏览代码

reordering and added demo for custom id

Michelle Bu 12 年之前
父节点
当前提交
6d5b9ea7cd
共有 6 个文件被更改,包括 235 次插入207 次删除
  1. 1 3
      demo/static/peer.html
  2. 78 68
      demo/static/peer.js
  3. 78 68
      dist/peer.js
  4. 0 0
      dist/peer.min.js
  5. 75 67
      lib/connection.js
  6. 3 1
      lib/peer.js

+ 1 - 3
demo/static/peer.html

@@ -11,8 +11,7 @@
 <script type="text/javascript" src="/peer.js"></script>
 <script type="text/javascript" src="/peer.js"></script>
 <script>
 <script>
 $(document).ready(function() {
 $(document).ready(function() {
-  connections = {};
-  originator = new Peer({ host: 'localhost', port: '9000', debug: true });
+  originator = new Peer({ id: 'michelle', host: 'localhost', port: '9000', debug: true });
   originator.on('ready', function(id) {
   originator.on('ready', function(id) {
     console.log(id);
     console.log(id);
   });
   });
@@ -23,7 +22,6 @@ $(document).ready(function() {
       console.log(data);
       console.log(data);
     });
     });
     connection.send('What is going on?');
     connection.send('What is going on?');
-    //connections[connection.metadata.username] = connection;
   });
   });
 });
 });
 
 

+ 78 - 68
demo/static/peer.js

@@ -862,7 +862,6 @@ function Peer(options) {
   this._apikey = options.apikey;
   this._apikey = options.apikey;
 
 
   // Check in with the server with ID or get an ID.
   // Check in with the server with ID or get an ID.
-//  this._startXhrStream();
   this._checkIn();
   this._checkIn();
 
 
   // Connections for this peer.
   // Connections for this peer.
@@ -1026,6 +1025,7 @@ Peer.prototype._socketInit = function() {
 };
 };
 
 
 
 
+/** Process queued calls to connect. */
 Peer.prototype._processQueue = function() {
 Peer.prototype._processQueue = function() {
   while (this._queued.length > 0) {
   while (this._queued.length > 0) {
     var cdata = this._queued.pop();
     var cdata = this._queued.pop();
@@ -1043,6 +1043,8 @@ Peer.prototype._cleanup = function() {
 };
 };
 
 
 
 
+/** Exposed connect function for users. Will try to connect later if user
+ * is waiting for an ID. */
 Peer.prototype.connect = function(peer, metadata, cb) {
 Peer.prototype.connect = function(peer, metadata, cb) {
   if (typeof metadata === 'function' && !cb) cb = metadata; metadata = false;
   if (typeof metadata === 'function' && !cb) cb = metadata; metadata = false;
 
 
@@ -1145,59 +1147,6 @@ DataConnection.prototype._setupDataChannel = function() {
 };
 };
 
 
 
 
-/** Exposed functions for Peer. */
-
-DataConnection.prototype.setSocketOpen = function() {
-  this._socketOpen = true;
-};
-
-DataConnection.prototype.handleSDP = function(message) {
-  var sdp = message.sdp;
-  if (util.browserisms != 'Firefox') {
-    sdp = new RTCSessionDescription(sdp);
-  }
-  var self = this;
-  this._pc.setRemoteDescription(sdp, function() {
-    util.log('Set remoteDescription: ' + message.type);
-    // Firefoxism
-    if (message.type === 'ANSWER' && util.browserisms === 'Firefox') {
-      self._pc.connectDataConnection(self.localPort, self.remotePort);
-      self._handleBroker('port', JSON.stringify({
-        type: 'PORT',
-        dst: self._peer,
-        src: self._id,
-        remote: self.localPort,
-        local: self.remotePort
-      }));
-    }
-  }, function(err) {
-    this._cb('Failed to setRemoteDescription');
-    util.log('Failed to setRemoteDescription, ', err);
-  });
-};
-
-
-DataConnection.prototype.handleCandidate = function(message) {
-  var candidate = new RTCIceCandidate(message.candidate);
-  this._pc.addIceCandidate(candidate);
-};
-
-
-DataConnection.prototype.handleLeave = function() {
-  util.log('Peer ' + this._peer + ' disconnected');
-  this._cleanup();
-};
-
-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);
-};
-
-
 /** Starts a PeerConnection and sets up handlers. */
 /** Starts a PeerConnection and sets up handlers. */
 DataConnection.prototype._startPeerConnection = function() {
 DataConnection.prototype._startPeerConnection = function() {
   util.log('Creating RTCPeerConnection: ', this.options.ice);
   util.log('Creating RTCPeerConnection: ', this.options.ice);
@@ -1376,6 +1325,29 @@ DataConnection.prototype._cleanup = function() {
 
 
 
 
 
 
+// Handles a DataChannel message.
+DataConnection.prototype._handleDataMessage = function(e) {
+  var self = this;
+  if (e.data.constructor === Blob) {
+    util.blobToArrayBuffer(e.data, function(ab) {
+      var data = BinaryPack.unpack(ab);
+      self.emit('data', data);
+    });
+  } else if (e.data.constructor === ArrayBuffer) {
+      var data = BinaryPack.unpack(e.data);
+      self.emit('data', data);
+  } else if (e.data.constructor === String) {
+      var ab = util.binaryStringToArrayBuffer(e.data);
+      var data = BinaryPack.unpack(ab);
+      self.emit('data', data);
+  }
+};
+
+
+/**
+ * Exposed functionality for users.
+ */
+
 /** Allows user to close connection. */
 /** Allows user to close connection. */
 DataConnection.prototype.close = function() {
 DataConnection.prototype.close = function() {
   this._cleanup();
   this._cleanup();
@@ -1402,22 +1374,60 @@ DataConnection.prototype.send = function(data) {
 };
 };
 
 
 
 
-// Handles a DataChannel message.
-DataConnection.prototype._handleDataMessage = function(e) {
+/**
+ * Exposed functions for Peer.
+ */
+
+DataConnection.prototype.setSocketOpen = function() {
+  this._socketOpen = true;
+};
+
+DataConnection.prototype.handleSDP = function(message) {
+  var sdp = message.sdp;
+  if (util.browserisms != 'Firefox') {
+    sdp = new RTCSessionDescription(sdp);
+  }
   var self = this;
   var self = this;
-  if (e.data.constructor === Blob) {
-    util.blobToArrayBuffer(e.data, function(ab) {
-      var data = BinaryPack.unpack(ab);
-      self.emit('data', data);
-    });
-  } else if (e.data.constructor === ArrayBuffer) {
-      var data = BinaryPack.unpack(e.data);
-      self.emit('data', data);
-  } else if (e.data.constructor === String) {
-      var ab = util.binaryStringToArrayBuffer(e.data);
-      var data = BinaryPack.unpack(ab);
-      self.emit('data', data);
+  this._pc.setRemoteDescription(sdp, function() {
+    util.log('Set remoteDescription: ' + message.type);
+    // Firefoxism
+    if (message.type === 'ANSWER' && util.browserisms === 'Firefox') {
+      self._pc.connectDataConnection(self.localPort, self.remotePort);
+      self._handleBroker('port', JSON.stringify({
+        type: 'PORT',
+        dst: self._peer,
+        src: self._id,
+        remote: self.localPort,
+        local: self.remotePort
+      }));
+    }
+  }, function(err) {
+    this._cb('Failed to setRemoteDescription');
+    util.log('Failed to setRemoteDescription, ', err);
+  });
+};
+
+
+DataConnection.prototype.handleCandidate = function(message) {
+  var candidate = new RTCIceCandidate(message.candidate);
+  this._pc.addIceCandidate(candidate);
+};
+
+
+DataConnection.prototype.handleLeave = function() {
+  util.log('Peer ' + this._peer + ' disconnected');
+  this._cleanup();
+};
+
+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);
 };
 };
 
 
+
+
 })(this);
 })(this);

+ 78 - 68
dist/peer.js

@@ -862,7 +862,6 @@ function Peer(options) {
   this._apikey = options.apikey;
   this._apikey = options.apikey;
 
 
   // Check in with the server with ID or get an ID.
   // Check in with the server with ID or get an ID.
-//  this._startXhrStream();
   this._checkIn();
   this._checkIn();
 
 
   // Connections for this peer.
   // Connections for this peer.
@@ -1026,6 +1025,7 @@ Peer.prototype._socketInit = function() {
 };
 };
 
 
 
 
+/** Process queued calls to connect. */
 Peer.prototype._processQueue = function() {
 Peer.prototype._processQueue = function() {
   while (this._queued.length > 0) {
   while (this._queued.length > 0) {
     var cdata = this._queued.pop();
     var cdata = this._queued.pop();
@@ -1043,6 +1043,8 @@ Peer.prototype._cleanup = function() {
 };
 };
 
 
 
 
+/** Exposed connect function for users. Will try to connect later if user
+ * is waiting for an ID. */
 Peer.prototype.connect = function(peer, metadata, cb) {
 Peer.prototype.connect = function(peer, metadata, cb) {
   if (typeof metadata === 'function' && !cb) cb = metadata; metadata = false;
   if (typeof metadata === 'function' && !cb) cb = metadata; metadata = false;
 
 
@@ -1145,59 +1147,6 @@ DataConnection.prototype._setupDataChannel = function() {
 };
 };
 
 
 
 
-/** Exposed functions for Peer. */
-
-DataConnection.prototype.setSocketOpen = function() {
-  this._socketOpen = true;
-};
-
-DataConnection.prototype.handleSDP = function(message) {
-  var sdp = message.sdp;
-  if (util.browserisms != 'Firefox') {
-    sdp = new RTCSessionDescription(sdp);
-  }
-  var self = this;
-  this._pc.setRemoteDescription(sdp, function() {
-    util.log('Set remoteDescription: ' + message.type);
-    // Firefoxism
-    if (message.type === 'ANSWER' && util.browserisms === 'Firefox') {
-      self._pc.connectDataConnection(self.localPort, self.remotePort);
-      self._handleBroker('port', JSON.stringify({
-        type: 'PORT',
-        dst: self._peer,
-        src: self._id,
-        remote: self.localPort,
-        local: self.remotePort
-      }));
-    }
-  }, function(err) {
-    this._cb('Failed to setRemoteDescription');
-    util.log('Failed to setRemoteDescription, ', err);
-  });
-};
-
-
-DataConnection.prototype.handleCandidate = function(message) {
-  var candidate = new RTCIceCandidate(message.candidate);
-  this._pc.addIceCandidate(candidate);
-};
-
-
-DataConnection.prototype.handleLeave = function() {
-  util.log('Peer ' + this._peer + ' disconnected');
-  this._cleanup();
-};
-
-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);
-};
-
-
 /** Starts a PeerConnection and sets up handlers. */
 /** Starts a PeerConnection and sets up handlers. */
 DataConnection.prototype._startPeerConnection = function() {
 DataConnection.prototype._startPeerConnection = function() {
   util.log('Creating RTCPeerConnection: ', this.options.ice);
   util.log('Creating RTCPeerConnection: ', this.options.ice);
@@ -1376,6 +1325,29 @@ DataConnection.prototype._cleanup = function() {
 
 
 
 
 
 
+// Handles a DataChannel message.
+DataConnection.prototype._handleDataMessage = function(e) {
+  var self = this;
+  if (e.data.constructor === Blob) {
+    util.blobToArrayBuffer(e.data, function(ab) {
+      var data = BinaryPack.unpack(ab);
+      self.emit('data', data);
+    });
+  } else if (e.data.constructor === ArrayBuffer) {
+      var data = BinaryPack.unpack(e.data);
+      self.emit('data', data);
+  } else if (e.data.constructor === String) {
+      var ab = util.binaryStringToArrayBuffer(e.data);
+      var data = BinaryPack.unpack(ab);
+      self.emit('data', data);
+  }
+};
+
+
+/**
+ * Exposed functionality for users.
+ */
+
 /** Allows user to close connection. */
 /** Allows user to close connection. */
 DataConnection.prototype.close = function() {
 DataConnection.prototype.close = function() {
   this._cleanup();
   this._cleanup();
@@ -1402,22 +1374,60 @@ DataConnection.prototype.send = function(data) {
 };
 };
 
 
 
 
-// Handles a DataChannel message.
-DataConnection.prototype._handleDataMessage = function(e) {
+/**
+ * Exposed functions for Peer.
+ */
+
+DataConnection.prototype.setSocketOpen = function() {
+  this._socketOpen = true;
+};
+
+DataConnection.prototype.handleSDP = function(message) {
+  var sdp = message.sdp;
+  if (util.browserisms != 'Firefox') {
+    sdp = new RTCSessionDescription(sdp);
+  }
   var self = this;
   var self = this;
-  if (e.data.constructor === Blob) {
-    util.blobToArrayBuffer(e.data, function(ab) {
-      var data = BinaryPack.unpack(ab);
-      self.emit('data', data);
-    });
-  } else if (e.data.constructor === ArrayBuffer) {
-      var data = BinaryPack.unpack(e.data);
-      self.emit('data', data);
-  } else if (e.data.constructor === String) {
-      var ab = util.binaryStringToArrayBuffer(e.data);
-      var data = BinaryPack.unpack(ab);
-      self.emit('data', data);
+  this._pc.setRemoteDescription(sdp, function() {
+    util.log('Set remoteDescription: ' + message.type);
+    // Firefoxism
+    if (message.type === 'ANSWER' && util.browserisms === 'Firefox') {
+      self._pc.connectDataConnection(self.localPort, self.remotePort);
+      self._handleBroker('port', JSON.stringify({
+        type: 'PORT',
+        dst: self._peer,
+        src: self._id,
+        remote: self.localPort,
+        local: self.remotePort
+      }));
+    }
+  }, function(err) {
+    this._cb('Failed to setRemoteDescription');
+    util.log('Failed to setRemoteDescription, ', err);
+  });
+};
+
+
+DataConnection.prototype.handleCandidate = function(message) {
+  var candidate = new RTCIceCandidate(message.candidate);
+  this._pc.addIceCandidate(candidate);
+};
+
+
+DataConnection.prototype.handleLeave = function() {
+  util.log('Peer ' + this._peer + ' disconnected');
+  this._cleanup();
+};
+
+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);
 };
 };
 
 
+
+
 })(this);
 })(this);

文件差异内容过多而无法显示
+ 0 - 0
dist/peer.min.js


+ 75 - 67
lib/connection.js

@@ -81,59 +81,6 @@ DataConnection.prototype._setupDataChannel = function() {
 };
 };
 
 
 
 
-/** Exposed functions for Peer. */
-
-DataConnection.prototype.setSocketOpen = function() {
-  this._socketOpen = true;
-};
-
-DataConnection.prototype.handleSDP = function(message) {
-  var sdp = message.sdp;
-  if (util.browserisms != 'Firefox') {
-    sdp = new RTCSessionDescription(sdp);
-  }
-  var self = this;
-  this._pc.setRemoteDescription(sdp, function() {
-    util.log('Set remoteDescription: ' + message.type);
-    // Firefoxism
-    if (message.type === 'ANSWER' && util.browserisms === 'Firefox') {
-      self._pc.connectDataConnection(self.localPort, self.remotePort);
-      self._handleBroker('port', JSON.stringify({
-        type: 'PORT',
-        dst: self._peer,
-        src: self._id,
-        remote: self.localPort,
-        local: self.remotePort
-      }));
-    }
-  }, function(err) {
-    this._cb('Failed to setRemoteDescription');
-    util.log('Failed to setRemoteDescription, ', err);
-  });
-};
-
-
-DataConnection.prototype.handleCandidate = function(message) {
-  var candidate = new RTCIceCandidate(message.candidate);
-  this._pc.addIceCandidate(candidate);
-};
-
-
-DataConnection.prototype.handleLeave = function() {
-  util.log('Peer ' + this._peer + ' disconnected');
-  this._cleanup();
-};
-
-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);
-};
-
-
 /** Starts a PeerConnection and sets up handlers. */
 /** Starts a PeerConnection and sets up handlers. */
 DataConnection.prototype._startPeerConnection = function() {
 DataConnection.prototype._startPeerConnection = function() {
   util.log('Creating RTCPeerConnection: ', this.options.ice);
   util.log('Creating RTCPeerConnection: ', this.options.ice);
@@ -312,6 +259,29 @@ DataConnection.prototype._cleanup = function() {
 
 
 
 
 
 
+// Handles a DataChannel message.
+DataConnection.prototype._handleDataMessage = function(e) {
+  var self = this;
+  if (e.data.constructor === Blob) {
+    util.blobToArrayBuffer(e.data, function(ab) {
+      var data = BinaryPack.unpack(ab);
+      self.emit('data', data);
+    });
+  } else if (e.data.constructor === ArrayBuffer) {
+      var data = BinaryPack.unpack(e.data);
+      self.emit('data', data);
+  } else if (e.data.constructor === String) {
+      var ab = util.binaryStringToArrayBuffer(e.data);
+      var data = BinaryPack.unpack(ab);
+      self.emit('data', data);
+  }
+};
+
+
+/**
+ * Exposed functionality for users.
+ */
+
 /** Allows user to close connection. */
 /** Allows user to close connection. */
 DataConnection.prototype.close = function() {
 DataConnection.prototype.close = function() {
   this._cleanup();
   this._cleanup();
@@ -338,20 +308,58 @@ DataConnection.prototype.send = function(data) {
 };
 };
 
 
 
 
-// Handles a DataChannel message.
-DataConnection.prototype._handleDataMessage = function(e) {
+/**
+ * Exposed functions for Peer.
+ */
+
+DataConnection.prototype.setSocketOpen = function() {
+  this._socketOpen = true;
+};
+
+DataConnection.prototype.handleSDP = function(message) {
+  var sdp = message.sdp;
+  if (util.browserisms != 'Firefox') {
+    sdp = new RTCSessionDescription(sdp);
+  }
   var self = this;
   var self = this;
-  if (e.data.constructor === Blob) {
-    util.blobToArrayBuffer(e.data, function(ab) {
-      var data = BinaryPack.unpack(ab);
-      self.emit('data', data);
-    });
-  } else if (e.data.constructor === ArrayBuffer) {
-      var data = BinaryPack.unpack(e.data);
-      self.emit('data', data);
-  } else if (e.data.constructor === String) {
-      var ab = util.binaryStringToArrayBuffer(e.data);
-      var data = BinaryPack.unpack(ab);
-      self.emit('data', data);
+  this._pc.setRemoteDescription(sdp, function() {
+    util.log('Set remoteDescription: ' + message.type);
+    // Firefoxism
+    if (message.type === 'ANSWER' && util.browserisms === 'Firefox') {
+      self._pc.connectDataConnection(self.localPort, self.remotePort);
+      self._handleBroker('port', JSON.stringify({
+        type: 'PORT',
+        dst: self._peer,
+        src: self._id,
+        remote: self.localPort,
+        local: self.remotePort
+      }));
+    }
+  }, function(err) {
+    this._cb('Failed to setRemoteDescription');
+    util.log('Failed to setRemoteDescription, ', err);
+  });
+};
+
+
+DataConnection.prototype.handleCandidate = function(message) {
+  var candidate = new RTCIceCandidate(message.candidate);
+  this._pc.addIceCandidate(candidate);
+};
+
+
+DataConnection.prototype.handleLeave = function() {
+  util.log('Peer ' + this._peer + ' disconnected');
+  this._cleanup();
+};
+
+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);
 };
 };
+
+

+ 3 - 1
lib/peer.js

@@ -23,7 +23,6 @@ function Peer(options) {
   this._apikey = options.apikey;
   this._apikey = options.apikey;
 
 
   // Check in with the server with ID or get an ID.
   // Check in with the server with ID or get an ID.
-//  this._startXhrStream();
   this._checkIn();
   this._checkIn();
 
 
   // Connections for this peer.
   // Connections for this peer.
@@ -187,6 +186,7 @@ Peer.prototype._socketInit = function() {
 };
 };
 
 
 
 
+/** Process queued calls to connect. */
 Peer.prototype._processQueue = function() {
 Peer.prototype._processQueue = function() {
   while (this._queued.length > 0) {
   while (this._queued.length > 0) {
     var cdata = this._queued.pop();
     var cdata = this._queued.pop();
@@ -204,6 +204,8 @@ Peer.prototype._cleanup = function() {
 };
 };
 
 
 
 
+/** Exposed connect function for users. Will try to connect later if user
+ * is waiting for an ID. */
 Peer.prototype.connect = function(peer, metadata, cb) {
 Peer.prototype.connect = function(peer, metadata, cb) {
   if (typeof metadata === 'function' && !cb) cb = metadata; metadata = false;
   if (typeof metadata === 'function' && !cb) cb = metadata; metadata = false;
 
 

部分文件因为文件数量过多而无法显示