无法在IE / Edge中对RTCPeerConnection使用'createDataChanne'-有解决方案吗?

时间:2018-12-14 19:02:43

标签: javascript google-chrome firefox internet-explorer-11 microsoft-edge

以下代码适用于Chrome / Firefox,但在IE / Edge中失败。我知道它不受支持,但是想知道是否有任何适用的回合解决方案?我正在尝试使其跨浏览器兼容,这是重要的一环。在这里的任何帮助将不胜感激!

(请放心,我是菜鸟。...只是在此方面寻求建议和帮助!)在此先谢谢您!

我在Internet Explorer中遇到的错误是: “对象不支持此操作”(pc =新的RTCPeerConnection({iceServers:[]})

我在Edge中遇到的错误是: “对象不支持属性或方法'createDataChannel'”

var serverFound = false;
var pc = null;
var appURL = ":5000/";
var testURL = appURL + "api/status";

$(document).ready(function() {

// Retry every 5 seconds.
   setInterval(function(){
      findMyIpAndConnectToSubnet();
   }, 5000);

// If we don't get a connection within 10 seconds, then prompt for it.
   setTimeout(function() {
      $("#serverentry").show();
   }, 10000);

   findMyIpAndConnectToSubnet();

   $("#btnConnect").on("click", function() {
      var ip = $("#ip").val();
      localStorage["lastConnection"] = ip;
      location.href = "http://"+ip+appURL;
   });
});

function findMyIpAndConnectToSubnet() {
   window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;   //compatibility for firefox and chrome
   pc = new RTCPeerConnection({iceServers:[]}), noop = function(){};
   pc.createDataChannel("");    //create a bogus data channel
   pc.createOffer(pc.setLocalDescription.bind(pc), noop);    // create offer and set local description
   pc.onicecandidate = function(ice){  //listen for candidate events
      if(!ice || !ice.candidate || !ice.candidate.candidate)  return;
      var myIP = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/.exec(ice.candidate.candidate)[1];
      discoverNeighborNPUServers(myIP);
//   console.log('my IP: ', myIP);
      pc.onicecandidate = noop;
   };
}


function discoverNeighborNPUServers(myIP) {

   // Try and speed through by reconnecting to the last one automatically.
   var lastConnection = localStorage["lastConnection"];
   if (lastConnection) {
      $("#ip").val(lastConnection);
      check(lastConnection);
   }

   var skipDiscovery = localStorage["skipDiscovery"];
   if (skipDiscovery != "true") {
      var ipasplit = myIP.split(".");
      ipasplit[3] = "0";

      while (parseInt(ipasplit[3]) < 256) {
         var newipa = ipasplit.join(".");
         check(newipa);
         ipasplit[3] ++;
      }
   }
}

function check(ip) {
   var xhr = new XMLHttpRequest();
   xhr.open('GET', "http://"+ip+testURL, true);
   xhr.timeout = 2000; // Don't wait very long
   xhr.onload = function() {
      if (xhr.status === 200 && !serverFound) {
         serverFound = true; // In case the server is on multiple IPs, just do the first one.
         localStorage["lastConnection"] = ip;
         location.href = "http://"+ip+appURL;
      }
      else {
      }
   };
   xhr.send();
}

1 个答案:

答案 0 :(得分:0)

您可以参考this thread并使用adapter.js