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;
ice.candidate.candidate.split('\r\n').forEach(function (line) {
if (line.indexOf("candidate")!==-1) {
var parts = line.split(' '),
addr = parts[4],
type = parts[7];
if (type === 'host') {
console.log(addr);
return addr;
}
}
});
pc.onicecandidate = noop;
};
In the above javascript, the console.log(addr) works but not the return. Please point me where I am doing wrong. I tried to wrap this in a function and that doesn't work it either.
return addr;will return from the.forEachcallback but that doesn't do anything at all - the.forEachjust continues with the next thing. Use a normal loop or perhaps.find()- note that.find()doesn't transform the value, it just returns the item from the array where the callback returned a truthy value.for (const line of ice.candidate.candidate.split('\r\n')) { ... }instead offorEachto make your code work.