Browse Source

next: xhr streaming

Michelle Bu 12 years ago
parent
commit
8eba759061
4 changed files with 34 additions and 43 deletions
  1. 1 1
      README.md
  2. 2 15
      lib/adapter.js
  3. 14 14
      lib/connection.js
  4. 17 13
      lib/peer.js

+ 1 - 1
README.md

@@ -35,7 +35,7 @@ var PeerServer = require('peer').PeerServer({ port: 80, debug: true });
 ```js
 var connections = {};
 
-p1 = new Peer({ server: 'localhost' });
+p1 = new Peer({ host: 'localhost', port: '8000' });
 p1.on('ready', function(id) {
   console.log(id); // => 'some_id_1'
 });

+ 2 - 15
lib/adapter.js

@@ -1,31 +1,18 @@
 var RTCPeerConnection = null;
 var getUserMedia = null;
 var attachMediaStream = null;
-var browserisms = null;
 
 if (navigator.mozGetUserMedia) {
-  browserisms = 'Firefox'
+  util.browserisms = 'Firefox'
 
   RTCPeerConnection = mozRTCPeerConnection;
-
   getUserMedia = navigator.mozGetUserMedia.bind(navigator);
-  attachMediaStream = function(element, stream) {
-    console.log("Attaching media stream");
-    element.mozSrcObject = stream;
-    element.play();
-  };
 } else if (navigator.webkitGetUserMedia) {
-  browserisms = 'Webkit'
+  util.browserisms = 'Webkit'
 
   RTCPeerConnection = webkitRTCPeerConnection;
-
   getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
-  attachMediaStream = function(element, stream) {
-    element.src = webkitURL.createObjectURL(stream);
-  };
 }
 
 exports.RTCPeerConnection = RTCPeerConnection;
 exports.getUserMedia = getUserMedia;
-exports.attachMediaStream = attachMediaStream;
-exports.browserisms = browserisms;

+ 14 - 14
lib/connection.js

@@ -12,7 +12,7 @@ function DataConnection(id, peer, socket, cb, options) {
   
   this._id = id;
   this._peer = peer;
-  this._originator = (options.sdp == undefined);
+  this._originator = (options.sdp === undefined);
   this._cb = cb;
  
   this.metadata = options.metadata;
@@ -21,7 +21,7 @@ function DataConnection(id, peer, socket, cb, options) {
   this._socket = socket;
 
   // Firefoxism: connectDataConnection ports.
-  if (browserisms == 'Firefox') {
+  if (util.browserisms === 'Firefox') {
     this._firefoxPortSetup();
   }
   //
@@ -34,7 +34,7 @@ function DataConnection(id, peer, socket, cb, options) {
   
   // Listen for negotiation needed
   // ** Chrome only.
-  if (browserisms === 'Webkit') {
+  if (util.browserisms === 'Webkit') {
     this._setupOffer();
   }
   
@@ -44,12 +44,12 @@ function DataConnection(id, peer, socket, cb, options) {
   var self = this;
   if (options.sdp) {
     this.handleSDP({type: 'OFFER', sdp: options.sdp});
-    if (browserisms !== 'Firefox') { 
+    if (util.browserisms !== 'Firefox') { 
       this._makeAnswer();
     }
   }
   
-  if (browserisms === 'Firefox') {
+  if (util.browserisms === 'Firefox') {
     this._firefoxAdditional();
   }
 };
@@ -83,14 +83,14 @@ DataConnection.prototype._setupDataChannel = function() {
 
 DataConnection.prototype.handleSDP = function(message) {
   var sdp = message.sdp;
-  if (browserisms != 'Firefox') {
+  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' && browserisms == 'Firefox') {
+    if (message.type === 'ANSWER' && util.browserisms === 'Firefox') {
       self._pc.connectDataConnection(self.localPort, self.remotePort);
       self._socket.send(JSON.stringify({
         type: 'PORT',
@@ -159,7 +159,7 @@ DataConnection.prototype._setupDataChannel = function() {
   var self = this;
   if (this._originator) {
     
-    if (browserisms == 'Webkit') {
+    if (util.browserisms === 'Webkit') {
 
       // TODO: figure out the right thing to do with this.
       this._pc.onstatechange = function() {
@@ -197,7 +197,7 @@ DataConnection.prototype._firefoxPortSetup = function() {
     this.localPort = util.randomPort();
   }
   this.remotePort = util.randomPort();
-  while (this.remotePort == this.localPort ||
+  while (this.remotePort === this.localPort ||
       DataConnection.usedPorts.indexOf(this.localPort) != -1) {
     this.remotePort = util.randomPort();
   }
@@ -208,7 +208,7 @@ DataConnection.prototype._firefoxPortSetup = function() {
 DataConnection.prototype._configureDataChannel = function() {
   var self = this;
   
-  if (browserisms === 'Firefox') {
+  if (util.browserisms === 'Firefox') {
     this._dc.binaryType = 'blob';
   }
   this._dc.onopen = function() {
@@ -308,7 +308,7 @@ DataConnection.prototype.close = function() {
 DataConnection.prototype.send = function(data) {
   var self = this;
   var blob = BinaryPack.pack(data);
-  if (browserisms == 'Webkit') {
+  if (util.browserisms === 'Webkit') {
     util.blobToBinaryString(blob, function(str){
       self._dc.send(str);
     });
@@ -321,15 +321,15 @@ DataConnection.prototype.send = function(data) {
 // Handles a DataChannel message.
 DataConnection.prototype._handleDataMessage = function(e) {
   var self = this;
-  if (e.data.constructor == Blob) {
+  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) {
+  } else if (e.data.constructor === ArrayBuffer) {
       var data = BinaryPack.unpack(e.data);
       self.emit('data', data);
-  } else if (e.data.constructor == String) {
+  } else if (e.data.constructor === String) {
       var ab = util.binaryStringToArrayBuffer(e.data);
       var data = BinaryPack.unpack(ab);
       self.emit('data', data);

+ 17 - 13
lib/peer.js

@@ -4,16 +4,28 @@ function Peer(options) {
 
   options = util.extend({
     debug: false,
-    server: 'localhost'
+    host: 'localhost',
+    port: 80
   }, options);
   this.options = options;
   util.debug = options.debug;
 
+  this._server = options.host + ':' + options.port;
+
+  // Ensure alphanumeric_-
+  if (options.id && !/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(options.id))
+    throw new Error('Peer ID can only contain alphanumerics, "_", and "-".');
+
   this._id = options.id;
+  // If no ID provided, get a unique ID from server. TODO
+
+  if (!this._id) {
   // Not used unless using cloud server.
-  this._api_key = options.api_key;
+  this._apikey = options.apikey;
 
-  this._socket = new WebSocket('ws://' + options.server);
+
+  // Must have an ID at this point.
+  this._socket = new WebSocket('ws://' + this._server + '/ws?');
   this._socketInit();
 
   // Connections for this peer.
@@ -22,9 +34,6 @@ function Peer(options) {
   // Queued connections to make.
   this._queued = [];
 
-  // If no ID provided, get a unique ID from server.
-  
-
   // Make sure connections are cleaned up.
   window.onbeforeunload = this._cleanup;
 };
@@ -39,11 +48,6 @@ Peer.prototype._socketInit = function() {
     var peer = message.src;
     var connection = self.connections[peer];
     switch (message.type) {
-      case 'ID':
-        self._id = message.id;
-        self._processQueue();
-        self.emit('ready', self._id);
-        break;
       case 'OFFER':
         var options = {
           metadata: message.metadata,
@@ -66,7 +70,7 @@ Peer.prototype._socketInit = function() {
         if (connection) connection.handleLeave();
         break;
       case 'PORT':
-        if (browserisms == 'Firefox') {
+        if (util.browserisms === 'Firefox') {
           connection.handlePort(message);
           break;
         }
@@ -76,7 +80,7 @@ Peer.prototype._socketInit = function() {
     }
   };
   this._socket.onopen = function() {
-    self._use_socket = true;
+    self._processQueue();
   };
 };