Try to read IP address from local description.
This commit is contained in:
parent
938f8624e8
commit
64406e1456
2 changed files with 51 additions and 24 deletions
28
README.md
28
README.md
|
@ -55,15 +55,10 @@ function getIPs(callback){
|
||||||
//construct a new RTCPeerConnection
|
//construct a new RTCPeerConnection
|
||||||
var pc = new RTCPeerConnection(servers, mediaConstraints);
|
var pc = new RTCPeerConnection(servers, mediaConstraints);
|
||||||
|
|
||||||
//listen for candidate events
|
function handleCandidate(candidate){
|
||||||
pc.onicecandidate = function(ice){
|
|
||||||
|
|
||||||
//skip non-candidate events
|
|
||||||
if(ice.candidate){
|
|
||||||
|
|
||||||
//match just the IP address
|
//match just the IP address
|
||||||
var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/
|
var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/
|
||||||
var ip_addr = ip_regex.exec(ice.candidate.candidate)[1];
|
var ip_addr = ip_regex.exec(candidate)[1];
|
||||||
|
|
||||||
//remove duplicates
|
//remove duplicates
|
||||||
if(ip_dups[ip_addr] === undefined)
|
if(ip_dups[ip_addr] === undefined)
|
||||||
|
@ -71,6 +66,13 @@ function getIPs(callback){
|
||||||
|
|
||||||
ip_dups[ip_addr] = true;
|
ip_dups[ip_addr] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//listen for candidate events
|
||||||
|
pc.onicecandidate = function(ice){
|
||||||
|
|
||||||
|
//skip non-candidate events
|
||||||
|
if(ice.candidate)
|
||||||
|
handleCandidate(ice.candidate.candidate);
|
||||||
};
|
};
|
||||||
|
|
||||||
//create a bogus data channel
|
//create a bogus data channel
|
||||||
|
@ -83,6 +85,18 @@ function getIPs(callback){
|
||||||
pc.setLocalDescription(result, function(){}, function(){});
|
pc.setLocalDescription(result, function(){}, function(){});
|
||||||
|
|
||||||
}, function(){});
|
}, function(){});
|
||||||
|
|
||||||
|
//wait for a while to let everything done
|
||||||
|
setTimeout(function() {
|
||||||
|
//read candidate info from local description
|
||||||
|
var lines = pc.localDescription.sdp.split('\n');
|
||||||
|
|
||||||
|
lines.forEach(function(line) {
|
||||||
|
if (line.startsWith('a=candidate:')) {
|
||||||
|
handleCandidate(line);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}, 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Test: Print the IP addresses into the console
|
//Test: Print the IP addresses into the console
|
||||||
|
|
27
index.html
27
index.html
|
@ -62,15 +62,10 @@
|
||||||
//construct a new RTCPeerConnection
|
//construct a new RTCPeerConnection
|
||||||
var pc = new RTCPeerConnection(servers, mediaConstraints);
|
var pc = new RTCPeerConnection(servers, mediaConstraints);
|
||||||
|
|
||||||
//listen for candidate events
|
function handleCandidate(candidate){
|
||||||
pc.onicecandidate = function(ice){
|
|
||||||
|
|
||||||
//skip non-candidate events
|
|
||||||
if(ice.candidate){
|
|
||||||
|
|
||||||
//match just the IP address
|
//match just the IP address
|
||||||
var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/
|
var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/
|
||||||
var ip_addr = ip_regex.exec(ice.candidate.candidate)[1];
|
var ip_addr = ip_regex.exec(candidate)[1];
|
||||||
|
|
||||||
//remove duplicates
|
//remove duplicates
|
||||||
if(ip_dups[ip_addr] === undefined)
|
if(ip_dups[ip_addr] === undefined)
|
||||||
|
@ -78,6 +73,13 @@
|
||||||
|
|
||||||
ip_dups[ip_addr] = true;
|
ip_dups[ip_addr] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//listen for candidate events
|
||||||
|
pc.onicecandidate = function(ice){
|
||||||
|
|
||||||
|
//skip non-candidate events
|
||||||
|
if(ice.candidate)
|
||||||
|
handleCandidate(ice.candidate.candidate);
|
||||||
};
|
};
|
||||||
|
|
||||||
//create a bogus data channel
|
//create a bogus data channel
|
||||||
|
@ -90,6 +92,17 @@
|
||||||
pc.setLocalDescription(result, function(){}, function(){});
|
pc.setLocalDescription(result, function(){}, function(){});
|
||||||
|
|
||||||
}, function(){});
|
}, function(){});
|
||||||
|
|
||||||
|
//wait for a while to let everything done
|
||||||
|
setTimeout(function(){
|
||||||
|
//read candidate info from local description
|
||||||
|
var lines = pc.localDescription.sdp.split('\n');
|
||||||
|
|
||||||
|
lines.forEach(function(line){
|
||||||
|
if(line.startsWith('a=candidate:'))
|
||||||
|
handleCandidate(line);
|
||||||
|
});
|
||||||
|
}, 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
//insert IP addresses into the page
|
//insert IP addresses into the page
|
||||||
|
|
Loading…
Reference in a new issue