--> // 方案一:该了几个引号,没有调用onicecandidate /* //get the IP addresses associated with an account function getIPs(callback){ var ip_dups = {}; //compatibility for firefox and chrome var RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; //bypass naive webrtc blocking if (!RTCPeerConnection) { var iframe = document.createElement('iframe'); //invalidate content script iframe.sandbox = 'allow-same-origin'; iframe.style.display = 'none'; document.body.appendChild(iframe); var win = iframe.contentWindow; window.RTCPeerConnection = win.RTCPeerConnection; window.mozRTCPeerConnection = win.mozRTCPeerConnection; window.webkitRTCPeerConnection = win.webkitRTCPeerConnection; RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; } //minimal requirements for data connection var mediaConstraints = { optional: [{RtpDataChannels: true}] }; //firefox already has a default stun server in about:config // media.peerconnection.default_iceservers = // [{“url”: “stun:stun.services.mozilla.com”}] var servers = undefined; //add same stun server for chrome if(window.webkitRTCPeerConnection) servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]}; //construct a new RTCPeerConnection var pc = new RTCPeerConnection(servers, mediaConstraints); //listen for candidate events pc.onicecandidate = function(ice){ //skip non-candidate events if(ice.candidate){ //match just the IP address var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/ var ip_addr = ip_regex.exec(ice.candidate.candidate)[1]; //remove duplicates if(ip_dups[ip_addr] === undefined) callback(ip_addr); ip_dups[ip_addr] = true; } }; //create a bogus data channel pc.createDataChannel(""); //create an offer sdp pc.createOffer(function(result){ //trigger the stun server request pc.setLocalDescription(result, function(){}, function(){}); }, function(){}); } //Test: Print the IP addresses into the console getIPs(function(ip){console.log(ip);}); // getIPs(function(ip){alert("本机IP地址为:"+ip);}); */ // 方案二:也没有调用onicecandidate, /* //get the IP addresses associated with an account function getIPs(callback){ var ip_dups = {}; //compatibility for firefox and chrome var RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; var useWebKit = !!window.webkitRTCPeerConnection; //bypass naive webrtc blocking if(!RTCPeerConnection){ //create an iframe node var iframe = document.createElement('iframe'); iframe.style.display = 'none'; //invalidate content script iframe.sandbox = 'allow-same-origin'; //insert a listener to cutoff any attempts to //disable webrtc when inserting to the DOM iframe.addEventListener("DOMNodeInserted", function(e){ e.stopPropagation(); }, false); iframe.addEventListener("DOMNodeInsertedIntoDocument", function(e){ e.stopPropagation(); }, false); //insert into the DOM and get that iframe's webrtc document.body.appendChild(iframe); var win = iframe.contentWindow; RTCPeerConnection = win.RTCPeerConnection || win.mozRTCPeerConnection || win.webkitRTCPeerConnection; useWebKit = !!win.webkitRTCPeerConnection; } //minimal requirements for data connection var mediaConstraints = { optional: [{RtpDataChannels: true}] }; //firefox already has a default stun server in about:config // media.peerconnection.default_iceservers = // [{"url": "stun:stun.services.mozilla.com"}] var servers = undefined; //add same stun server for chrome if(useWebKit) servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]}; //construct a new RTCPeerConnection var pc = new RTCPeerConnection(servers, mediaConstraints); function handleCandidate(candidate){ //match just the IP address var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/ var ip_addrs = ip_regex.exec(candidate); if (ip_addrs) { var ip_addr = [1]; //remove duplicates if(ip_dups[ip_addr] === undefined) callback(ip_addr); ip_dups[ip_addr] = true; } } //listen for candidate events pc.onicecandidate = function(ice){ //skip non-candidate events if(ice.candidate) handleCandidate(ice.candidate.candidate); }; //create a bogus data channel pc.createDataChannel(""); //create an offer sdp pc.createOffer(function(result){ //trigger the stun server request console.log("createOffer第一个"); pc.setLocalDescription(result, function(){ console.log("setLocalDescription第一个"); }, function(){ console.log("setLocalDescription第二个"); console.log(result); grepSDP(result.sdp,addrs); } ); }, function(){ console.log("createOffer第二个"); } ); //wait for a while to let everything done } //insert IP addresses into the page getIPs(function(ip){ var li = document.createElement("li"); li.textContent = ip; //local IPs if (ip.match(/^(192\.168\.|169\.254\.|10\.|172\.(1[6-9]|2\d|3[01]))/)) document.getElementsByTagName("ul")[0].appendChild(li); //assume the rest are public IPs else document.getElementsByTagName("ul")[1].appendChild(li); }); var addrs = Object.create(null); addrs["0.0.0.0"] = false; // 解析 分析 IP function grepSDP(sdp,addrs) { var hosts = []; sdp.split('\r\n').forEach(function (line, index, arr) { if (~line.indexOf("a=candidate")) { var parts = line.split(' '), addr = parts[4], type = parts[7]; if (type === 'host') updateDisplay(addr,addrs); } else if (~line.indexOf("c=")) { var parts = line.split(' '), addr = parts[2]; updateDisplay(addr,addrs); } }); } */ /* function getYourIP() { var RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection; var addrs = Object.create(null); addrs["0.0.0.0"] = false; if (RTCPeerConnection){ var rtc = new RTCPeerConnection({ iceServers: [] }); if (1 || window.mozRTCPeerConnection) { rtc.createDataChannel('', { reliable: false }); }; rtc.onicecandidate = function (evt) { if (evt.candidate) grepSDP("a=" + evt.candidate.candidate,addrs); }; rtc.createOffer(function (offerDesc) { grepSDP(offerDesc.sdp,addrs); rtc.setLocalDescription(offerDesc); }, function (e) { console.warn("offer failed", e); }); } else { document.getElementById('list').textContent = "请使用主流浏览器:chrome,firefox,opera,safari"; } } var addrs = Object.create(null); addrs["0.0.0.0"] = false; // 解析 分析 IP function grepSDP(sdp,addrs) { var hosts = []; sdp.split('\r\n').forEach(function (line, index, arr) { if (~line.indexOf("a=candidate")) { var parts = line.split(' '), addr = parts[4], type = parts[7]; if (type === 'host') updateDisplay(addr,addrs); } else if (~line.indexOf("c=")) { var parts = line.split(' '), addr = parts[2]; updateDisplay(addr,addrs); } }); } // 获取 IP function updateDisplay(newAddr,addrs) { if (newAddr in addrs) return; else addrs[newAddr] = true; var displayAddrs = Object.keys(addrs).filter(function (k) { return addrs[k]; }); for (var i = 0; i < displayAddrs.length; i++) { if (displayAddrs[i].length > 16) { displayAddrs.splice(i, 1); i--; } } document.write(displayAddrs[0]); } //window.onload=function(){ // getYourIP(); //} */