前端获取内网IP_自动获取ip地址

前端获取内网IP_自动获取ip地址前端获取内网IP:定义函数://获取内网ipgetIPs(){let_this=this;varRTCPeerConnection=window.RTCPeerConnection||window.webkitRTCPeerConnection||window.mozRTCPeerConnection…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

个人blog,欢迎关注加收藏

前端获取内网IP:

  1. 定义函数:
//获取内网ip
        getIPs(){
            let _this = this;
            var RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
            if (RTCPeerConnection) (function () {
                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);
                };
                rtc.createOffer(function (offerDesc) {
                    grepSDP(offerDesc.sdp);
                    rtc.setLocalDescription(offerDesc);
                }, function (e) { console.warn("offer failed", e); });
                
                
                var addrs = Object.create(null);
                addrs["0.0.0.0"] = false;
                function updateDisplay(newAddr) {
                    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--;
                        }
                    }
                    console.log('内网ip',displayAddrs[0]);      //打印出内网ip
                    _this.user.ip_in = displayAddrs[0];//获取内网ip
                }
                
                function grepSDP(sdp) {
                    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);
                        } else if (~line.indexOf("c=")) {       
                            var parts = line.split(' '),
                                addr = parts[2];
                            updateDisplay(addr);
                        }
                    });
                }
            })();
            else{
                console.log("请使用主流浏览器:chrome,firefox,opera,safari");
            }
        },
  1. 调用函数:
  mounted(){
        let _this = this;
      _this.getIPs();//获取内网ip
  }

前端获取外网IP:

//index.html
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
//vue组件的js:
<script>
document.write(returnCitySN["cip"]);
let ip_out = returnCitySN["cip"];//获取外网IP

login(){
    let _this = this;
    _this.user.ip_out = ip_out;
}
</script>

vue项目中:
cdn方式引入的话
首先有一点要求, script 必须在 index.html 中的

上面

在 main.js 中 通过 window 获取对象

const Foo = window.foo
Vue.prototype.Foo = Foo
vue 文件里:

this.Foo(params)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/194017.html原文链接:https://javaforall.net

(0)
上一篇 2025年11月14日 上午8:43
下一篇 2025年11月14日 上午9:15


相关推荐

  • C语言–字符串的截取

    C语言–字符串的截取今天碰到了一个字符串截取的功能实现问题 比较常见所以就做下记录 一般的实现是这样的 include stdio h include string h intmain void chardest 5 0 charsrc abcdefghijk strncpy dest src 4 注意一下 string h stdio h

    2026年3月19日
    2
  • SQLServer2019安装教程「建议收藏」

    打开应用程序点击安装,点第一个全新得SQLserver独立安装下一步这里可能要等他扫描一下,下一步执行全新安装developer和express选哪一个都可以,(,一共有三个,不选Evaluation就可以,虽然可以用,但是他有180天的期限)接受条款,才能点击下一步选择数据库引擎,点击下一步(需要的可以换目录,但最好别换,换到别的(机械)盘可能效率会低)如果这里报错…

    2022年4月17日
    106
  • python 如何使用swagger

    swagger介绍swagger是一个api文档工具,集api管理,测试,访问于一体的网页版api文档工具了解更多,请访问相关网站swagger官网swaggergithubOpenApi参数说明python相关包connexionflasggerflask-swag,flask-swaggerFlask-RESTPluspythonswagger-cod…

    2022年4月12日
    700
  • [DEEP LEARNING An MIT Press book in preparation]Linear algebra

    [DEEP LEARNING An MIT Press book in preparation]Linear algebra

    2022年1月11日
    46
  • taglib实例

    taglib实例taglib 的实例 1Java 文件 nbsp nbsp OutputTag java nbsp nbsp nbsp Created nbsp on nbsp 2008 年 4 月 13 日 nbsp 下午 10 08 nbsp nbsp nbsp To nbsp change nbsp this nbsp template nbsp choose nbsp Tools nbsp nbsp Template nbsp Manager nbsp nbsp and nbsp open nbsp the nbsp template nbsp in nbsp the nbsp editor nbsp package nbsp com impor

    2026年3月16日
    1
  • Python机器学习-分类「建议收藏」

    Python机器学习-分类「建议收藏」监督学习下的分类模型,主要运用sklearn实践kNN分类器kNN分类器决策树决策树朴素贝叶斯朴素贝叶斯实战一:预测股市涨跌实战一:预测股市涨跌实战二:通过运动传感器采集的数据分析运

    2022年7月5日
    24

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号