Skip to content

Instantly share code, notes, and snippets.

@femoru
Created January 15, 2018 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save femoru/ca54e8de8f0ebcd3189c7d3010f4f6f2 to your computer and use it in GitHub Desktop.
Save femoru/ca54e8de8f0ebcd3189c7d3010f4f6f2 to your computer and use it in GitHub Desktop.
Get local client ipv4 using RTCPeerConnection
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