首页 > 其他 > 详细

vue中获取客户端IP地址

时间:2020-04-27 22:39:35      阅读:2832      评论:0      收藏:0      [点我收藏+]

获取ip方法

技术分享图片
export function getUserIP(onNewIP) {
  let MyPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
  let pc = new MyPeerConnection({
    iceServers: []
  });
  let noop = () => {
  };
  let localIPs = {};
  let ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g;
  let iterateIP = (ip) => {
    if (!localIPs[ip]) onNewIP(ip);
    localIPs[ip] = true;
  };
  pc.createDataChannel(‘‘);
  pc.createOffer().then((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((reason) => {
  });
  pc.onicecandidate = (ice) => {
    if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
    ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
  };
}
getUserIP

调用getUserIP获取ip

<template>
    <h1>{{ ip }}</h1>
</template>
<script>
import getUserIP from ./utils
  export default {
    data() {
      return {
        ip: ‘‘
      };
    },
    methods: {
      
    },
    mounted() {
      getUserIP((ip) => {
        this.ip = ip;
      });
    }
  }
</script>
<style lang="scss" scoped>
</style>

 

vue中获取客户端IP地址

原文:https://www.cnblogs.com/xianquan/p/12790668.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!