Jelajahi Sumber

revert onnegotiationneed

Jairo 7 tahun lalu
induk
melakukan
8cd0ea5bdf
1 mengubah file dengan 54 tambahan dan 46 penghapusan
  1. 54 46
      lib/util.js

+ 54 - 46
lib/util.js

@@ -1,17 +1,17 @@
-var defaultConfig = {'iceServers': [{ 'url': 'stun:stun.l.google.com:19302' }]};
+var defaultConfig = { iceServers: [{ url: "stun:stun.l.google.com:19302" }] };
 var dataCount = 1;
 var dataCount = 1;
 
 
-var BinaryPack = require('js-binarypack');
-var RTCPeerConnection = require('./adapter').RTCPeerConnection;
+var BinaryPack = require("js-binarypack");
+var RTCPeerConnection = require("./adapter").RTCPeerConnection;
 
 
 var util = {
 var util = {
   noop: function() {},
   noop: function() {},
 
 
-  CLOUD_HOST: '0.peerjs.com',
+  CLOUD_HOST: "0.peerjs.com",
   CLOUD_PORT: 9000,
   CLOUD_PORT: 9000,
 
 
   // Browsers that need chunking:
   // Browsers that need chunking:
-  chunkedBrowsers: {'Chrome': 1},
+  chunkedBrowsers: { Chrome: 1 },
   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.
   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
   // Logging logic
@@ -26,10 +26,10 @@ var util = {
     }
     }
     util.log = util.warn = util.error = util.noop;
     util.log = util.warn = util.error = util.noop;
     if (util.logLevel > 0) {
     if (util.logLevel > 0) {
-      util.error = util._printWith('ERROR');
+      util.error = util._printWith("ERROR");
     }
     }
     if (util.logLevel > 1) {
     if (util.logLevel > 1) {
-      util.warn = util._printWith('WARNING');
+      util.warn = util._printWith("WARNING");
     }
     }
     if (util.logLevel > 2) {
     if (util.logLevel > 2) {
       util.log = util._print;
       util.log = util._print;
@@ -37,7 +37,9 @@ var util = {
   },
   },
   setLogFunction: function(fn) {
   setLogFunction: function(fn) {
     if (fn.constructor !== Function) {
     if (fn.constructor !== Function) {
-      util.warn('The log function you passed in is not a function. Defaulting to regular logs.');
+      util.warn(
+        "The log function you passed in is not a function. Defaulting to regular logs."
+      );
     } else {
     } else {
       util._print = fn;
       util._print = fn;
     }
     }
@@ -50,13 +52,13 @@ var util = {
       util._print.apply(util, copy);
       util._print.apply(util, copy);
     };
     };
   },
   },
-  _print: function () {
+  _print: function() {
     var err = false;
     var err = false;
     var copy = Array.prototype.slice.call(arguments);
     var copy = Array.prototype.slice.call(arguments);
-    copy.unshift('PeerJS: ');
-    for (var i = 0, l = copy.length; i < l; i++){
+    copy.unshift("PeerJS: ");
+    for (var i = 0, l = copy.length; i < l; i++) {
       if (copy[i] instanceof Error) {
       if (copy[i] instanceof Error) {
-        copy[i] = '(' + copy[i].name + ') ' + copy[i].message;
+        copy[i] = "(" + copy[i].name + ") " + copy[i].message;
         err = true;
         err = true;
       }
       }
     }
     }
@@ -71,20 +73,20 @@ var util = {
   // Returns the current browser.
   // Returns the current browser.
   browser: (function() {
   browser: (function() {
     if (window.mozRTCPeerConnection) {
     if (window.mozRTCPeerConnection) {
-      return 'Firefox';
+      return "Firefox";
     } else if (window.webkitRTCPeerConnection) {
     } else if (window.webkitRTCPeerConnection) {
-      return 'Chrome';
+      return "Chrome";
     } else if (window.RTCPeerConnection) {
     } else if (window.RTCPeerConnection) {
-      return 'Supported';
+      return "Supported";
     } else {
     } else {
-      return 'Unsupported';
+      return "Unsupported";
     }
     }
   })(),
   })(),
   //
   //
 
 
   // Lists which features are supported
   // Lists which features are supported
   supports: (function() {
   supports: (function() {
-    if (typeof RTCPeerConnection === 'undefined') {
+    if (typeof RTCPeerConnection === "undefined") {
       return {};
       return {};
     }
     }
 
 
@@ -93,11 +95,13 @@ var util = {
 
 
     var binaryBlob = false;
     var binaryBlob = false;
     var sctp = false;
     var sctp = false;
-    var onnegotiationneeded = false;
+    var onnegotiationneeded = !!window.webkitRTCPeerConnection;
 
 
     var pc, dc;
     var pc, dc;
     try {
     try {
-      pc = new RTCPeerConnection(defaultConfig, {optional: [{RtpDataChannels: true}]});
+      pc = new RTCPeerConnection(defaultConfig, {
+        optional: [{ RtpDataChannels: true }]
+      });
     } catch (e) {
     } catch (e) {
       data = false;
       data = false;
       audioVideo = false;
       audioVideo = false;
@@ -105,7 +109,7 @@ var util = {
 
 
     if (data) {
     if (data) {
       try {
       try {
-        dc = pc.createDataChannel('_PEERJSTEST');
+        dc = pc.createDataChannel("_PEERJSTEST");
       } catch (e) {
       } catch (e) {
         data = false;
         data = false;
       }
       }
@@ -114,20 +118,21 @@ var util = {
     if (data) {
     if (data) {
       // Binary test
       // Binary test
       try {
       try {
-        dc.binaryType = 'blob';
+        dc.binaryType = "blob";
         binaryBlob = true;
         binaryBlob = true;
-      } catch (e) {
-      }
+      } catch (e) {}
 
 
       // Reliable test.
       // Reliable test.
       // Unfortunately Chrome is a bit unreliable about whether or not they
       // Unfortunately Chrome is a bit unreliable about whether or not they
       // support reliable.
       // support reliable.
       var reliablePC = new RTCPeerConnection(defaultConfig, {});
       var reliablePC = new RTCPeerConnection(defaultConfig, {});
       try {
       try {
-        var reliableDC = reliablePC.createDataChannel('_PEERJSRELIABLETEST', {});
+        var reliableDC = reliablePC.createDataChannel(
+          "_PEERJSRELIABLETEST",
+          {}
+        );
         sctp = reliableDC.reliable;
         sctp = reliableDC.reliable;
-      } catch (e) {
-      }
+      } catch (e) {}
       reliablePC.close();
       reliablePC.close();
     }
     }
 
 
@@ -170,7 +175,7 @@ var util = {
       sctp: sctp,
       sctp: sctp,
       onnegotiationneeded: onnegotiationneeded
       onnegotiationneeded: onnegotiationneeded
     };
     };
-  }()),
+  })(),
   //
   //
 
 
   // Ensure alphanumeric ids
   // Ensure alphanumeric ids
@@ -184,7 +189,6 @@ var util = {
     return !key || /^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(key);
     return !key || /^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(key);
   },
   },
 
 
-
   debug: false,
   debug: false,
 
 
   inherits: function(ctor, superCtor) {
   inherits: function(ctor, superCtor) {
@@ -199,8 +203,8 @@ var util = {
     });
     });
   },
   },
   extend: function(dest, source) {
   extend: function(dest, source) {
-    for(var key in source) {
-      if(source.hasOwnProperty(key)) {
+    for (var key in source) {
+      if (source.hasOwnProperty(key)) {
         dest[key] = source[key];
         dest[key] = source[key];
       }
       }
     }
     }
@@ -209,31 +213,33 @@ var util = {
   pack: BinaryPack.pack,
   pack: BinaryPack.pack,
   unpack: BinaryPack.unpack,
   unpack: BinaryPack.unpack,
 
 
-  log: function () {
+  log: function() {
     if (util.debug) {
     if (util.debug) {
       var err = false;
       var err = false;
       var copy = Array.prototype.slice.call(arguments);
       var copy = Array.prototype.slice.call(arguments);
-      copy.unshift('PeerJS: ');
-      for (var i = 0, l = copy.length; i < l; i++){
+      copy.unshift("PeerJS: ");
+      for (var i = 0, l = copy.length; i < l; i++) {
         if (copy[i] instanceof Error) {
         if (copy[i] instanceof Error) {
-          copy[i] = '(' + copy[i].name + ') ' + copy[i].message;
+          copy[i] = "(" + copy[i].name + ") " + copy[i].message;
           err = true;
           err = true;
         }
         }
       }
       }
-      err ? console.error.apply(console, copy) : console.log.apply(console, copy);
+      err
+        ? console.error.apply(console, copy)
+        : console.log.apply(console, copy);
     }
     }
   },
   },
 
 
   setZeroTimeout: (function(global) {
   setZeroTimeout: (function(global) {
     var timeouts = [];
     var timeouts = [];
-    var messageName = 'zero-timeout-message';
+    var messageName = "zero-timeout-message";
 
 
     // Like setTimeout, but only takes a function argument.	 There's
     // Like setTimeout, but only takes a function argument.	 There's
     // no time argument (always zero) and no arguments (you have to
     // no time argument (always zero) and no arguments (you have to
     // use a closure).
     // use a closure).
     function setZeroTimeoutPostMessage(fn) {
     function setZeroTimeoutPostMessage(fn) {
       timeouts.push(fn);
       timeouts.push(fn);
-      global.postMessage(messageName, '*');
+      global.postMessage(messageName, "*");
     }
     }
 
 
     function handleMessage(event) {
     function handleMessage(event) {
@@ -247,12 +253,12 @@ var util = {
       }
       }
     }
     }
     if (global.addEventListener) {
     if (global.addEventListener) {
-      global.addEventListener('message', handleMessage, true);
+      global.addEventListener("message", handleMessage, true);
     } else if (global.attachEvent) {
     } else if (global.attachEvent) {
-      global.attachEvent('onmessage', handleMessage);
+      global.attachEvent("onmessage", handleMessage);
     }
     }
     return setZeroTimeoutPostMessage;
     return setZeroTimeoutPostMessage;
-  }(window)),
+  })(window),
 
 
   // Binary stuff
   // Binary stuff
 
 
@@ -260,7 +266,7 @@ var util = {
   chunk: function(bl) {
   chunk: function(bl) {
     var chunks = [];
     var chunks = [];
     var size = bl.size;
     var size = bl.size;
-    var start = index = 0;
+    var start = (index = 0);
     var total = Math.ceil(size / util.chunkedMTU);
     var total = Math.ceil(size / util.chunkedMTU);
     while (start < size) {
     while (start < size) {
       var end = Math.min(size, start + util.chunkedMTU);
       var end = Math.min(size, start + util.chunkedMTU);
@@ -282,14 +288,14 @@ var util = {
     return chunks;
     return chunks;
   },
   },
 
 
-  blobToArrayBuffer: function(blob, cb){
+  blobToArrayBuffer: function(blob, cb) {
     var fr = new FileReader();
     var fr = new FileReader();
     fr.onload = function(evt) {
     fr.onload = function(evt) {
       cb(evt.target.result);
       cb(evt.target.result);
     };
     };
     fr.readAsArrayBuffer(blob);
     fr.readAsArrayBuffer(blob);
   },
   },
-  blobToBinaryString: function(blob, cb){
+  blobToBinaryString: function(blob, cb) {
     var fr = new FileReader();
     var fr = new FileReader();
     fr.onload = function(evt) {
     fr.onload = function(evt) {
       cb(evt.target.result);
       cb(evt.target.result);
@@ -303,13 +309,15 @@ var util = {
     }
     }
     return byteArray.buffer;
     return byteArray.buffer;
   },
   },
-  randomToken: function () {
-    return Math.random().toString(36).substr(2);
+  randomToken: function() {
+    return Math.random()
+      .toString(36)
+      .substr(2);
   },
   },
   //
   //
 
 
   isSecure: function() {
   isSecure: function() {
-    return location.protocol === 'https:';
+    return location.protocol === "https:";
   }
   }
 };
 };