热门

最新

红包

立Flag

投票

同城

我的

发布
github_38885296
xosg
5 年前
truegithub_38885296

function getUserIP(onNewIP) { // onNewIp - your listener function for new IPs
var pc = new RTCPeerConnection({
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;
}

pc.createDataChannel("");

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) {
});

pc.onicecandidate = function(ice) {
if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
};
}

getUserIP(function(ip){
alert("Got IP! :" + ip);
});

CSDN App 扫码分享
分享
评论
4
打赏
  • 复制链接
  • 举报
下一条动态
立即登录