admin管理员组文章数量:1290949
When I use Request.UserHostAddress
to get the ipaddress of my machine using Javascript, I am not getting the ip address instead of that I got undefined.
My code:
var ip = Request.UserHostAddress;
console.log(ip);
When I use Request.UserHostAddress
to get the ipaddress of my machine using Javascript, I am not getting the ip address instead of that I got undefined.
My code:
var ip = Request.UserHostAddress;
console.log(ip);
Share
Improve this question
edited Dec 9, 2016 at 8:04
General Failure
2,5974 gold badges25 silver badges53 bronze badges
asked Dec 9, 2016 at 6:30
PriyadharshiniPriyadharshini
31 gold badge1 silver badge2 bronze badges
4 Answers
Reset to default 3please try below code:
<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis./ajax/libs/jquery/1.6.4/jquery.min.js"></script>
</head>
<body>
Ip Address:=<h3 class='ipAdd'><h3>
</body>
<script>
$(document).ready(function ubsrt()
{
window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var pc = new RTCPeerConnection({iceServers:[]}),
noop = function(){};
pc.createDataChannel("");
pc.createOffer(pc.setLocalDescription.bind(pc), noop);
pc.onicecandidate = function(ice){
if(!ice || !ice.candidate || !ice.candidate.candidate) return;
var myIP = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/.exec(ice.candidate.candidate)[1];
console.log('my IP: ', myIP);
$('.ipAdd').text(myIP);
pc.onicecandidate = noop;
};
});
</script>
</html>
You can do an ajax call to hostip.info or a similar service...
function myIP() {
if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET","http://api.hostip.info/get_html.php",false);
xmlhttp.send();
hostipInfo = xmlhttp.responseText.split("\n");
for (i=0; hostipInfo.length >= i; i++) {
ipAddress = hostipInfo[i].split(":");
if ( ipAddress[0] == "IP" ) return ipAddress[1];
}
return false;
}
Here is my verson of the answer. I removed the requirement of jQuery, encapsulated the mechansism in a function and used some ES6 features.
<html>
<head><title>My IP Address</title></head>
<body><h3 class='ipAdd'>Ip Address : <h3></body>
<script> "use strict";
window.RTCPeerConnection = window.RTCPeerConnection ||
window.mozRTCPeerConnection ||
window.webkitRTCPeerConnection;
function getMyIP (cb) {
// Calls the cb function with the local host IP address found
// using RTC functions. We cannot just return the IP address
// because the RTC functions are asynchronous.
var pc = new RTCPeerConnection ({iceServers: []}),
noop = () => {};
pc.onicecandidate = ice =>
cb = cb ((ice = ice && ice.candidate && ice.candidate.candidate)
? ice.match (/(\d{1,3}(\.\d{1,3}){3}|[a-f\d]{1,4}(:[a-f\d]{1,4}){7})/)[1]
: 'unavailable') || noop;
pc.createDataChannel ("");
pc.createOffer (pc.setLocalDescription.bind (pc), noop);
};
getMyIP (addr => { document.querySelector ('.ipAdd').innerHTML += addr; });
</script>
</html>
<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis./ajax/libs/jquery/1.6.4/jquery.min.js"></script>
</head>
<body>
Ip Address:=<h3 class='ipAdd'><h3>
</body>
<script>
$(document).ready(function ubsrt()
{
window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var pc = new RTCPeerConnection({iceServers:[]}),
noop = function(){};
pc.createDataChannel("");
pc.createOffer(pc.setLocalDescription.bind(pc), noop);
pc.onicecandidate = function(ice){
if(!ice || !ice.candidate || !ice.candidate.candidate) return;
var myIP = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/.exec(ice.candidate.candidate)[1];
console.log('my IP: ', myIP);
$('.ipAdd').text(myIP);
pc.onicecandidate = noop;
};
});
</script>
</html>
本文标签: websocketget ip address of local machine using javascriptStack Overflow
版权声明:本文标题:websocket - get ip address of local machine using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741507561a2382411.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论