소스 검색

Chrome/FF DataChannel interop \o/

Michelle Bu 11 년 전
부모
커밋
e9ca38c626
3개의 변경된 파일23개의 추가작업 그리고 10개의 파일을 삭제
  1. 13 3
      lib/dataconnection.js
  2. 6 3
      lib/negotiator.js
  3. 4 4
      lib/util.js

+ 13 - 3
lib/dataconnection.js

@@ -32,6 +32,11 @@ function DataConnection(peer, provider, options) {
   // For storing large data.
   this._chunkedData = {};
 
+  if (this.options._payload) {
+    this._peerBrowser = this.options._payload.browser;
+    this._peerSupports = this.options._payload.supports;
+  }
+
   Negotiator.startConnection(
     this,
     this.options._payload || {
@@ -163,8 +168,10 @@ DataConnection.prototype.send = function(data, chunked) {
     var utf8 = (this.serialization === 'binary-utf8');
     var blob = util.pack(data, utf8);
 
-    // For Chrome-Firefox interoperability, we need to make Firefox "chunking" the data it sends out. Future sophistication of this approach could make the decision on chunking dependent on the remote browser.
-    if (/*util.browser !== 'Firefox' && */!chunked && blob.size > util.chunkedMTU) {
+    // For Chrome-Firefox interoperability, we need to make Firefox "chunk"
+    // the data it sends out.
+    var needsChunking = util.chunkedBrowsers.indexOf(this._peerBrowser) !== -1 || util.browser !== 'Firefox';
+    if (needsChunking && !chunked && blob.size > util.chunkedMTU) {
       this._sendChunks(blob);
       return;
     }
@@ -174,7 +181,7 @@ DataConnection.prototype.send = function(data, chunked) {
       util.blobToBinaryString(blob, function(str) {
         self._bufferedSend(str);
       });
-    } else if (!util.supports.binaryBlob) {
+    } else if (!util.supports.binaryBlob || (this._peerSupports && !this._peerSupports.binaryBlob)) {
       // We only do this if we really need to (e.g. blobs are not supported),
       // because this conversion is costly.
       util.blobToArrayBuffer(blob, function(ab) {
@@ -241,6 +248,9 @@ DataConnection.prototype.handleMessage = function(message) {
 
   switch (message.type) {
     case 'ANSWER':
+      this._peerBrowser = payload.browser;
+      this._peerSupports = payload.supports;
+
       // Forward to negotiator
       Negotiator.handleSDP(message.type, this, payload.sdp);
       break;

+ 6 - 3
lib/negotiator.js

@@ -214,11 +214,12 @@ Negotiator._makeOffer = function(connection) {
           sdp: offer,
           type: connection.type,
           label: connection.label,
+          connectionId: connection.id,
           reliable: connection.reliable,
           serialization: connection.serialization,
           metadata: connection.metadata,
-          connectionId: connection.id,
-          sctp: util.supports.sctp
+          supports: util.supports,
+          browser: util.browser
         },
         dst: connection.peer
       });
@@ -249,7 +250,9 @@ Negotiator._makeAnswer = function(connection) {
         payload: {
           sdp: answer,
           type: connection.type,
-          connectionId: connection.id
+          connectionId: connection.id,
+          supports: util.supports,
+          browser: util.browser
         },
         dst: connection.peer
       });

+ 4 - 4
lib/util.js

@@ -6,7 +6,10 @@ var util = {
 
   CLOUD_HOST: '0.peerjs.com',
   CLOUD_PORT: 9000,
-  chunkedMTU: 16300, // The original 60000 bytes setting does not work when sending data from Firefox to Chrome, which is "cut off" after 16384 bytes and delivered individually. I suspect a bug in either Chrome or Firefox behind this.
+
+  // Browsers that need chunking:
+  chunkedBrowsers: ['Chrome'],
+  chunkedMTU: 16300, // The original 60000 bytes setting does not work when sending data from Firefox to Chrome, which is "cut off" after 16384 bytes and delivered individually.
 
   // Logging logic
   logLevel: 0,
@@ -107,14 +110,11 @@ var util = {
 
     if (data) {
       // Binary test
-      // Commented out the "binary blob" feature detection. Firefox supports sending Blobs, whereas Chrome does not. Hence we agree on the common denominator (which is using plain ArrayBuffers).
-      /*
       try {
         dc.binaryType = 'blob';
         binaryBlob = true;
       } catch (e) {
       }
-      */
 
       // Reliable test.
       // Unfortunately Chrome is a bit unreliable about whether or not they