【发布时间】:2018-12-19 17:46:50
【问题描述】:
以下代码适用于 Chrome/Firefox,但在 IE/Edge 中失败。我知道它不受支持,但我想知道是否有任何适用的工作或解决方案?我试图让它跨浏览器兼容,这是一个重要的部分。这里的任何帮助将不胜感激!
(放轻松,我是菜鸟......只是在这方面寻找一些建议和帮助!)提前谢谢你!
我在 Internet Explorer 中遇到的错误是: “对象不支持此操作”(pc = new 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();
}
【问题讨论】:
标签: javascript google-chrome firefox internet-explorer-11 microsoft-edge