Răsfoiți Sursa

fixed connection object for id-less peers

Michelle Bu 12 ani în urmă
părinte
comite
e44ca65d17
8 a modificat fișierele cu 130 adăugiri și 64 ștergeri
  1. 4 1
      demo/static/peer.html
  2. 40 16
      demo/static/peer.js
  3. 6 15
      demo/static/peer1.html
  4. 40 16
      dist/peer.js
  5. 0 0
      dist/peer.min.js
  6. 19 5
      lib/connection.js
  7. 10 8
      lib/peer.js
  8. 11 3
      lib/socket.js

+ 4 - 1
demo/static/peer.html

@@ -21,10 +21,13 @@ $(document).ready(function() {
   originator.on('connection', function(connection) {
     x = connection;
     console.log('connection');
+
+    connection.on('open', function() {
+      connection.send('What is going on?');
+    });
     connection.on('data', function(data) {
       console.log(data);
     });
-    connection.send('What is going on?');
   });
 });
 

+ 40 - 16
demo/static/peer.js

@@ -889,7 +889,9 @@ Peer.prototype._startSocket = function() {
     self._handleServerJSONMessage(data);
   });
   this._socket.on('open', function() {
-    self._processQueue();
+    if (this.id) {
+      self._processQueue();
+    }
   });
   this._socket.on('error', function(error) {
     util.log(error);
@@ -939,6 +941,7 @@ Peer.prototype._handleServerJSONMessage = function(message) {
       this.emit('connection', connection, message.metadata);
       break;
     case 'EXPIRE':
+      connection = this.connections[message.expired];
       if (connection) {
         connection.close();
         connection.emit('Could not connect to peer ' + connection.peer);
@@ -978,8 +981,8 @@ Peer.prototype._handleServerJSONMessage = function(message) {
 /** Process queued calls to connect. */
 Peer.prototype._processQueue = function() {
   while (this._queued.length > 0) {
-    var cdata = this._queued.pop();
-    this.connect.apply(this, cdata);
+    var conn = this._queued.pop();
+    conn.initialize(this.id);
   }
 };
 
@@ -1012,19 +1015,18 @@ Peer.prototype._attachConnectionListeners = function(connection) {
 // TODO: pause XHR streaming when not in use and start again when this is
 // called.
 Peer.prototype.connect = function(peer, metadata, options) {
-  if (!this.id) {
-    this._queued.push(Array.prototype.slice.apply(arguments));
-    return;
-  }
-
   options = util.extend({
     metadata: metadata,
     config: this._options.config,
   }, options);
+
   var connection = new DataConnection(this.id, peer, this._socket, options);
   this._attachConnectionListeners(connection);
 
   this.connections[peer] = connection;
+  if (!this.id) {
+    this._queued.push(connection);
+  }
   return connection;
 };
 
@@ -1056,7 +1058,20 @@ function DataConnection(id, peer, socket, options) {
 
   this._originator = (options.sdp === undefined);
   this._socket = socket;
+  this._sdp = options.sdp;
+
+  // TODO: consider no-oping this method:
+  if (!!this.id) {
+    this.initialize();
+  }
+};
 
+util.inherits(DataConnection, EventEmitter);
+
+DataConnection.prototype.initialize = function(id) {
+  if (!!id) {
+    this.id = id;
+  }
   // Firefoxism: connectDataConnection ports.
   /*if (util.browserisms === 'Firefox') {
     this._firefoxPortSetup();
@@ -1070,7 +1085,7 @@ function DataConnection(id, peer, socket, options) {
   
   // Listen for negotiation needed
   // ** Chrome only.
-  if (util.browserisms !== 'Firefox') {
+  if (util.browserisms !== 'Firefox' && !!this.id) {
     this._setupOffer();
   }
   
@@ -1078,17 +1093,18 @@ function DataConnection(id, peer, socket, options) {
   this._setupDataChannel();
   
   var self = this;
-  if (options.sdp) {
-    this.handleSDP({ type: 'OFFER', sdp: options.sdp });
+  if (this._sdp) {
+    this.handleSDP({ type: 'OFFER', sdp: this._sdp });
   }
   
   // Makes offer if Firefox
   /*if (util.browserisms === 'Firefox') {
     this._firefoxAdditional();
   }*/
-};
 
-util.inherits(DataConnection, EventEmitter);
+  // No-op this.
+  this.initialize = function() {};
+}
 
 DataConnection.prototype._setupOffer = function() {
   var self = this;
@@ -1422,9 +1438,15 @@ Socket.prototype._startWebSocket = function() {
 
   var self = this;
   this._socket.onmessage = function(event) {
+    var data;
     try {
-      self.emit('message', JSON.parse(event.data));
+      data = JSON.parse(event.data);
     } catch(e) {
+      data = event.data;
+    }
+    if (data.constructor == Object) {
+      self.emit('message', data);
+    } else {
       util.log('Invalid server message', event.data);
     }
   };
@@ -1473,7 +1495,9 @@ Socket.prototype._handleStream = function(http, pad) {
   }
 
   if (this._index === undefined) {
-    this._index = pad ? 2 : 1;
+    // TODO
+    this._index = 2;
+    //this._index = pad ? 2 : 1;
   }
   
   if (http.responseText === null) {
@@ -1509,7 +1533,7 @@ Socket.prototype._handleHTTPErrors = function(message) {
       break;
     case 'HTTP-ERROR':
       // this.emit('error', 'Something went wrong.');
-      util.log('XHR ended in error state');
+      util.log('XHR ended in error or the websocket connected first.');
       break;
     default:
       this.emit('message', message);

+ 6 - 15
demo/static/peer1.html

@@ -11,26 +11,17 @@
 <script type="text/javascript" src="/peer.js"></script>
 <script>
 $(document).ready(function() {
-  connections = {};
   $('#connect').click(function() {
     var source = $('#source').val();
 
-    sink = new Peer({ host: 'localhost', port: '9000', debug: true });
-      sink.connect(source, { username: 'michelle' }, function(err, connection) {
-        x = connection;
-        console.log('got connection');
-        console.log(connection);
-
-        connection.on('data', function(data) {
-          console.log(data);
-        });
-
-        connection.send('Hi there!');
-        connections[source] = connection;
-      });
-    sink.on('ready', function() {
+    sink = new Peer({ key: 'r6l1dggn5hebqpvi', host: 'localhost', port: '9000', debug: true });
+    connection = sink.connect(source, { username: 'michelle_spoof' })
+    connection.on('data', function(data) {
+      console.log(data);
+      connection.send('Hi there!');
     });
   });
+
 });
 
 </script>

+ 40 - 16
dist/peer.js

@@ -889,7 +889,9 @@ Peer.prototype._startSocket = function() {
     self._handleServerJSONMessage(data);
   });
   this._socket.on('open', function() {
-    self._processQueue();
+    if (this.id) {
+      self._processQueue();
+    }
   });
   this._socket.on('error', function(error) {
     util.log(error);
@@ -939,6 +941,7 @@ Peer.prototype._handleServerJSONMessage = function(message) {
       this.emit('connection', connection, message.metadata);
       break;
     case 'EXPIRE':
+      connection = this.connections[message.expired];
       if (connection) {
         connection.close();
         connection.emit('Could not connect to peer ' + connection.peer);
@@ -978,8 +981,8 @@ Peer.prototype._handleServerJSONMessage = function(message) {
 /** Process queued calls to connect. */
 Peer.prototype._processQueue = function() {
   while (this._queued.length > 0) {
-    var cdata = this._queued.pop();
-    this.connect.apply(this, cdata);
+    var conn = this._queued.pop();
+    conn.initialize(this.id);
   }
 };
 
@@ -1012,19 +1015,18 @@ Peer.prototype._attachConnectionListeners = function(connection) {
 // TODO: pause XHR streaming when not in use and start again when this is
 // called.
 Peer.prototype.connect = function(peer, metadata, options) {
-  if (!this.id) {
-    this._queued.push(Array.prototype.slice.apply(arguments));
-    return;
-  }
-
   options = util.extend({
     metadata: metadata,
     config: this._options.config,
   }, options);
+
   var connection = new DataConnection(this.id, peer, this._socket, options);
   this._attachConnectionListeners(connection);
 
   this.connections[peer] = connection;
+  if (!this.id) {
+    this._queued.push(connection);
+  }
   return connection;
 };
 
@@ -1056,7 +1058,20 @@ function DataConnection(id, peer, socket, options) {
 
   this._originator = (options.sdp === undefined);
   this._socket = socket;
+  this._sdp = options.sdp;
+
+  // TODO: consider no-oping this method:
+  if (!!this.id) {
+    this.initialize();
+  }
+};
 
+util.inherits(DataConnection, EventEmitter);
+
+DataConnection.prototype.initialize = function(id) {
+  if (!!id) {
+    this.id = id;
+  }
   // Firefoxism: connectDataConnection ports.
   /*if (util.browserisms === 'Firefox') {
     this._firefoxPortSetup();
@@ -1070,7 +1085,7 @@ function DataConnection(id, peer, socket, options) {
   
   // Listen for negotiation needed
   // ** Chrome only.
-  if (util.browserisms !== 'Firefox') {
+  if (util.browserisms !== 'Firefox' && !!this.id) {
     this._setupOffer();
   }
   
@@ -1078,17 +1093,18 @@ function DataConnection(id, peer, socket, options) {
   this._setupDataChannel();
   
   var self = this;
-  if (options.sdp) {
-    this.handleSDP({ type: 'OFFER', sdp: options.sdp });
+  if (this._sdp) {
+    this.handleSDP({ type: 'OFFER', sdp: this._sdp });
   }
   
   // Makes offer if Firefox
   /*if (util.browserisms === 'Firefox') {
     this._firefoxAdditional();
   }*/
-};
 
-util.inherits(DataConnection, EventEmitter);
+  // No-op this.
+  this.initialize = function() {};
+}
 
 DataConnection.prototype._setupOffer = function() {
   var self = this;
@@ -1422,9 +1438,15 @@ Socket.prototype._startWebSocket = function() {
 
   var self = this;
   this._socket.onmessage = function(event) {
+    var data;
     try {
-      self.emit('message', JSON.parse(event.data));
+      data = JSON.parse(event.data);
     } catch(e) {
+      data = event.data;
+    }
+    if (data.constructor == Object) {
+      self.emit('message', data);
+    } else {
       util.log('Invalid server message', event.data);
     }
   };
@@ -1473,7 +1495,9 @@ Socket.prototype._handleStream = function(http, pad) {
   }
 
   if (this._index === undefined) {
-    this._index = pad ? 2 : 1;
+    // TODO
+    this._index = 2;
+    //this._index = pad ? 2 : 1;
   }
   
   if (http.responseText === null) {
@@ -1509,7 +1533,7 @@ Socket.prototype._handleHTTPErrors = function(message) {
       break;
     case 'HTTP-ERROR':
       // this.emit('error', 'Something went wrong.');
-      util.log('XHR ended in error state');
+      util.log('XHR ended in error or the websocket connected first.');
       break;
     default:
       this.emit('message', message);

Fișier diff suprimat deoarece este prea mare
+ 0 - 0
dist/peer.min.js


+ 19 - 5
lib/connection.js

@@ -20,7 +20,20 @@ function DataConnection(id, peer, socket, options) {
 
   this._originator = (options.sdp === undefined);
   this._socket = socket;
+  this._sdp = options.sdp;
 
+  // TODO: consider no-oping this method:
+  if (!!this.id) {
+    this.initialize();
+  }
+};
+
+util.inherits(DataConnection, EventEmitter);
+
+DataConnection.prototype.initialize = function(id) {
+  if (!!id) {
+    this.id = id;
+  }
   // Firefoxism: connectDataConnection ports.
   /*if (util.browserisms === 'Firefox') {
     this._firefoxPortSetup();
@@ -34,7 +47,7 @@ function DataConnection(id, peer, socket, options) {
   
   // Listen for negotiation needed
   // ** Chrome only.
-  if (util.browserisms !== 'Firefox') {
+  if (util.browserisms !== 'Firefox' && !!this.id) {
     this._setupOffer();
   }
   
@@ -42,17 +55,18 @@ function DataConnection(id, peer, socket, options) {
   this._setupDataChannel();
   
   var self = this;
-  if (options.sdp) {
-    this.handleSDP({ type: 'OFFER', sdp: options.sdp });
+  if (this._sdp) {
+    this.handleSDP({ type: 'OFFER', sdp: this._sdp });
   }
   
   // Makes offer if Firefox
   /*if (util.browserisms === 'Firefox') {
     this._firefoxAdditional();
   }*/
-};
 
-util.inherits(DataConnection, EventEmitter);
+  // No-op this.
+  this.initialize = function() {};
+}
 
 DataConnection.prototype._setupOffer = function() {
   var self = this;

+ 10 - 8
lib/peer.js

@@ -50,7 +50,9 @@ Peer.prototype._startSocket = function() {
     self._handleServerJSONMessage(data);
   });
   this._socket.on('open', function() {
-    self._processQueue();
+    if (this.id) {
+      self._processQueue();
+    }
   });
   this._socket.on('error', function(error) {
     util.log(error);
@@ -100,6 +102,7 @@ Peer.prototype._handleServerJSONMessage = function(message) {
       this.emit('connection', connection, message.metadata);
       break;
     case 'EXPIRE':
+      connection = this.connections[message.expired];
       if (connection) {
         connection.close();
         connection.emit('Could not connect to peer ' + connection.peer);
@@ -139,8 +142,8 @@ Peer.prototype._handleServerJSONMessage = function(message) {
 /** Process queued calls to connect. */
 Peer.prototype._processQueue = function() {
   while (this._queued.length > 0) {
-    var cdata = this._queued.pop();
-    this.connect.apply(this, cdata);
+    var conn = this._queued.pop();
+    conn.initialize(this.id);
   }
 };
 
@@ -173,19 +176,18 @@ Peer.prototype._attachConnectionListeners = function(connection) {
 // TODO: pause XHR streaming when not in use and start again when this is
 // called.
 Peer.prototype.connect = function(peer, metadata, options) {
-  if (!this.id) {
-    this._queued.push(Array.prototype.slice.apply(arguments));
-    return;
-  }
-
   options = util.extend({
     metadata: metadata,
     config: this._options.config,
   }, options);
+
   var connection = new DataConnection(this.id, peer, this._socket, options);
   this._attachConnectionListeners(connection);
 
   this.connections[peer] = connection;
+  if (!this.id) {
+    this._queued.push(connection);
+  }
   return connection;
 };
 

+ 11 - 3
lib/socket.js

@@ -75,9 +75,15 @@ Socket.prototype._startWebSocket = function() {
 
   var self = this;
   this._socket.onmessage = function(event) {
+    var data;
     try {
-      self.emit('message', JSON.parse(event.data));
+      data = JSON.parse(event.data);
     } catch(e) {
+      data = event.data;
+    }
+    if (data.constructor == Object) {
+      self.emit('message', data);
+    } else {
       util.log('Invalid server message', event.data);
     }
   };
@@ -126,7 +132,9 @@ Socket.prototype._handleStream = function(http, pad) {
   }
 
   if (this._index === undefined) {
-    this._index = pad ? 2 : 1;
+    // TODO
+    this._index = 2;
+    //this._index = pad ? 2 : 1;
   }
   
   if (http.responseText === null) {
@@ -162,7 +170,7 @@ Socket.prototype._handleHTTPErrors = function(message) {
       break;
     case 'HTTP-ERROR':
       // this.emit('error', 'Something went wrong.');
-      util.log('XHR ended in error state');
+      util.log('XHR ended in error or the websocket connected first.');
       break;
     default:
       this.emit('message', message);

Unele fișiere nu au fost afișate deoarece prea multe fișiere au fost modificate în acest diff