Created
January 15, 2018 13:18
-
-
Save femoru/ca54e8de8f0ebcd3189c7d3010f4f6f2 to your computer and use it in GitHub Desktop.
Get local client ipv4 using RTCPeerConnection
This file contains 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
window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; //compatibility for firefox and chrome | |
var pc = new RTCPeerConnection({ iceServers: [] }); | |
var noop = function() {}; | |
pc.createDataChannel(""); //create a bogus data channel | |
pc.createOffer(pc.setLocalDescription.bind(pc), noop); // create offer and set local description | |
pc.onicecandidate = function(ice) { //listen for candidate events | |
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]; | |
//sessionStorage.setItem('IP',myIP) | |
pc.onicecandidate = noop; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment