Created
April 9, 2016 12:24
-
-
Save Hexa/717d1c2b2564c51579cad6bdf4782e74 to your computer and use it in GitHub Desktop.
JS でローカルのアドレスを取得
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>IP Address</title> | |
</head> | |
<body> | |
<div> | |
<h3>IP Address</h3> | |
<ul id="list"></ul> | |
</div> | |
<script> | |
var RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection; | |
var RTCSessionDescription = window.RTCSessionDescription || window.RTCSessionDescription; | |
var RTCIceCandidate = window.RTCIceCandidate || window.RTCIceCandidate; | |
var config = {"iceServers": []}; | |
var pc = new RTCPeerConnection(config); | |
var addresses = []; | |
pc.onicecandidate = function(event) { | |
if (event.candidate) { | |
var candidate = event.candidate.candidate; | |
if (candidate.match("typ host")) { | |
var address = candidate.split(" ")[4]; | |
addresses.push(address); | |
} | |
} else { | |
var filtered_addresses = addresses.filter(function(element, index, self) { | |
return self.indexOf(element) === index; | |
}); | |
filtered_addresses.forEach( | |
function(address) { | |
var list = document.getElementById("list"); | |
var li = document.createElement("li"); | |
li.textContent = address; | |
list.appendChild(li); | |
}) | |
dataChannel.close(); | |
} | |
}; | |
var dataChannel = pc.createDataChannel("label"); | |
pc.createOffer( | |
function (sessionDescription) { | |
pc.setLocalDescription(sessionDescription, | |
function() {}, | |
function(error) { console.log(error); }); | |
}, | |
function (error) { console.log(error); }, | |
{}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment