Good day! There is a code. which receives through webrtc ip addresses of the user and displays the values ​​in the console. How can I get the values ​​of ip addresses in the array? I tried instead of getIPs (function (ip) {console.log (ip);}); functions sub var arr = []; getIPs (function (ip) {arr.push (ip);}); But for some reason, when getting the values ​​of the array, I get udentified.

//get the IP addresses associated with an account function getIPs(callback){ var ip_dups = {}; //compatibility for firefox and chrome var RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; var mediaConstraints = { optional: [{RtpDataChannels: true}] }; //firefox already has a default stun server in about:config // media.peerconnection.default_iceservers = // [{"url": "stun:stun.services.mozilla.com"}] var servers = undefined; //add same stun server for chrome if(window.webkitRTCPeerConnection) servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]}; //construct a new RTCPeerConnection var pc = new RTCPeerConnection(servers, mediaConstraints); //listen for candidate events pc.onicecandidate = function(ice){ //skip non-candidate events if(ice.candidate){ //match just the IP address var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/ var ip_addr = ip_regex.exec(ice.candidate.candidate)[1]; //remove duplicates if(ip_dups[ip_addr] === undefined) callback(ip_addr); ip_dups[ip_addr] = true; } }; //create a bogus data channel pc.createDataChannel(""); //create an offer sdp pc.createOffer(function(result){ //trigger the stun server request pc.setLocalDescription(result, function(){}, function(){}); }, function(){}); } //Test: Print the IP addresses into the console getIPs(function(ip){console.log(ip);}); 

    2 answers 2

    Try this code here.

     //get the IP addresses associated with an account function getIPs(callback) { getIPs.ip_dups = {}; //compatibility for firefox and chrome var RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; var mediaConstraints = { optional: [{ RtpDataChannels: true }] }; //firefox already has a default stun server in about:config // media.peerconnection.default_iceservers = // [{"url": "stun:stun.services.mozilla.com"}] var servers = undefined; //add same stun server for chrome if (window.webkitRTCPeerConnection) servers = { iceServers: [{ urls: "stun:stun.services.mozilla.com" }] }; //construct a new RTCPeerConnection var pc = new RTCPeerConnection(servers, mediaConstraints); //listen for candidate events pc.onicecandidate = function(ice) { //skip non-candidate events if (ice.candidate) { //match just the IP address var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/ var ip_addr = ip_regex.exec(ice.candidate.candidate)[1]; //remove duplicates if (getIPs.ip_dups[ip_addr] === undefined) { getIPs.ip_dups[ip_addr] = true; callback(ip_addr); } } }; //create a bogus data channel pc.createDataChannel(""); //create an offer sdp pc.createOffer(function(result) { //trigger the stun server request pc.setLocalDescription(result, function() {}, function() {}); }, function() {}); } //Test: Print the IP addresses into the console getIPs(function(ip) { console.log(getIPs.ip_dups); }); 

    • Thank! It turned out - lidochkaeasy

    Collect data with a callback into an array instead of output to the console.

     var ips = [] getIPs(function(ip) { ips.push(ip); }); console.log(ips) 
    • Thank! It turned out - lidochkaeasy
    • When console.log(ips) executed, the console.log(ips) array may be empty. - Stepan Kasyanenko
    • yes, this was exactly the problem, it is empty if it is written like that, since the callback function works later. - lidochkaeasy
    • I tried to deduce the specific value of the array and received - undefiend. But when it’s outputting an array to the console in chrome, the browser hinted that the array is being filled later. - lidochkaeasy