Safariがバージョン11に更新されたので、WebRTC APIを使用できます。しかし、クライアントIPアドレス(ローカルIP、つまり192.168.1.10)を取得しようとしていますが、結果はありません。サファリでWebアドレスを取得する
私が使用しているコードは、いくつかのガイドで見つけることができるコードです。 Safariより前にこのAPIと互換性のあるChromeとFirefoxで同じコードが動作します。それはこのようなものです:
/**
* Get the user IP throught the webkitRTCPeerConnection
* @param onNewIP {Function} listener function to expose the IP locally
* @return undefined
*/
function getUserIP(onNewIP) { // onNewIp - your listener function for new IPs
//compatibility for firefox and chrome
var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var pc = new myPeerConnection({
iceServers: []
}),
noop = function() {},
localIPs = {},
ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
key;
function iterateIP(ip) {
if (!localIPs[ip]) onNewIP(ip);
localIPs[ip] = true;
}
//create a bogus data channel
pc.createDataChannel("");
// create offer and set local description
pc.createOffer().then(function(sdp) {
sdp.sdp.split('\n').forEach(function(line) {
if (line.indexOf('candidate') < 0) return;
line.match(ipRegex).forEach(iterateIP);
});
pc.setLocalDescription(sdp, noop, noop);
}).catch(function(reason) {
// An error occurred, so handle the failure to connect
});
//listen for candidate events
pc.onicecandidate = function(ice) {
if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
};
}
// Usage
getUserIP(function(ip){
alert("Got IP! :" + ip);
});
私は、デバッグしてきたと私はice.candidate
が定義されていないことを考え出したので、コード内で反復処理する任意のIPがありません。
アイデアや代替品はありますか?
ありがとうございます。
ありがとうございます。私の目標ではありませんが、それは一時的な解決策として動作します – daniherculano
公衆インターネットからローカルIpを取得するソリューションはありません。今日はサンフランシスコで開催されたkrankygeek.comイベントでリンゴ代理人によって再び言及されました。他のブラウザは最終的に仕様の新しいバージョンと整合し、ローカル候補を無効にし、HTML5準拠になる可能性があります。 –