Google:chrome://flags/
Edge:edge://flags/
getIp() {
if (typeof window != "undefined") {
var RTCPeerConnection =
window.RTCPeerConnection ||
window.mozRTCPeerConnection ||
window.webkitRTCPeerConnection;
if (RTCPeerConnection)
(() => {
var rtc = new RTCPeerConnection();
rtc.createDataChannel(""); //创建一个可以发送任意数据的数据通道
rtc.createOffer(
(offerDesc) => {
//创建并存储一个sdp数据
rtc.setLocalDescription(offerDesc);
},
(e) => {
console.log(e);
}
);
rtc.onicecandidate = (evt) => {
//监听candidate事件
if (evt.candidate) {
let ip_rule = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/;
// 判断ip_addr是否获取成功
if (ip_rule.exec(evt.candidate.candidate)) {
this.address.ip = ip_rule.exec(evt.candidate.candidate)[1];
} else {
// 引导用户打开浏览器的指定配置获取ip
this.dialogVisibleIp = true;
}
console.log("ip_addr:", this.address); //打印获取的IP地址
}
};
})();
else {
console.log("没有找到");
}
}
},
|