首页 > Web开发 > 详细

html利用js获取局域网内的本机IP

时间:2019-02-25 19:01:52      阅读:1735      评论:0      收藏:0      [点我收藏+]
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<title>JavaScript获取客户端IP[利用新浪接口]</title>
</head>
<body>
<script type="text/javascript">
function getIPs(callback){
    var ip_dups = {};
    var RTCPeerConnection = window.RTCPeerConnection
        || window.mozRTCPeerConnection
        || window.webkitRTCPeerConnection;
    var useWebKit = !!window.webkitRTCPeerConnection;


    if(!RTCPeerConnection){
      
        var win = iframe.contentWindow;
        RTCPeerConnection = win.RTCPeerConnection
            || win.mozRTCPeerConnection
            || win.webkitRTCPeerConnection;
        useWebKit = !!win.webkitRTCPeerConnection;
    }


    var mediaConstraints = {
        optional: [{RtpDataChannels: true}]
    };


    var servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]};


    var pc = new RTCPeerConnection(servers, mediaConstraints);


    function handleCandidate(candidate){
        var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
        var tt=ip_regex.exec(candidate);
      
       if(tt==null||tt.length<2){
       return false;
       }
        var ip_addr = tt[1];
        
      


        if(ip_dups[ip_addr] === undefined)
            callback(ip_addr);


        ip_dups[ip_addr] = true;
    }


    pc.onicecandidate = function(ice){


        if(ice.candidate)
            handleCandidate(ice.candidate.candidate);
    };


    pc.createDataChannel("");


    pc.createOffer(function(result){


        pc.setLocalDescription(result, function(){}, function(){});


    }, function(){});


    setTimeout(function(){
        var lines = pc.localDescription.sdp.split(‘\n‘);


        lines.forEach(function(line){
            if(line.indexOf(‘a=candidate:‘) === 0)
                handleCandidate(line);
        });
    }, 1000);
}
getIPs(function(ip){
alert(ip);
});
</script>
</body>
</html>

 

html利用js获取局域网内的本机IP

原文:https://www.cnblogs.com/leeego-123/p/10432554.html

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