0

i have this function:

const clientsIpAdress = (onNewIP) => {
  const MyPeerConnection =
    window.RTCPeerConnection ||
    window.mozRTCPeerConnection ||
    window.webkitRTCPeerConnection;
  const pc = new MyPeerConnection({
    iceServers: []
  });
  const noop = () => {};
  const localIPs = {};
  const ipRegex =
    /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g;

  const iterateIP = (ip) => {
    if (!localIPs[ip]) onNewIP(ip);
    localIPs[ip] = true;
  };
  pc.createDataChannel('');
  pc.createOffer().then((sdp) => {
    sdp.sdp.split('\n').forEach((line) => {
      if (line.indexOf('candidate') < 0) return;
      line.match(ipRegex).forEach(iterateIP);
    });

    pc.setLocalDescription(sdp, noop, noop);
  });
  pc.onicecandidate = (ice) => {
    if (!ice || !ice.candidate ||
      !ice.candidate.candidate ||
      !ice.candidate.candidate.match(ipRegex)) return;
    ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
  };
};
export default clientsIpAdress;

when i'm writing const hey = clientsIpAdress(ip => {return ip}) it shows me undefined. Why is this happening? May i use a callback? I'm a little bit confused

RamAlx
  • 6,976
  • 23
  • 58
  • 106
  • You already use a callback. Only it's asynchronous, and the return value of the callback isn't the return value of the `clientsIpAddress` function call. Use `clientsIpAddress(hey => { /* whatever you want to do with hey */ });` – Bergi Jun 26 '17 at 14:51
  • I want to return the onNewIp adress. Will i write clientsIpAddress(hey => { hey.ip })? – RamAlx Jun 26 '17 at 14:53
  • I can't understand... – RamAlx Jun 26 '17 at 14:57
  • console.log(clientsIpAdress(function(ip) {return ip})); Still it's undefined. What the hell am doing wrong? :/ – RamAlx Jun 26 '17 at 15:07
  • I know it's duplicate, i've read the post but i cant understand what's wrong. That;s why i posted the question – RamAlx Jun 26 '17 at 15:11
  • It's fundamentally impossible to return the address. You need to use `clientsIpAdress(ip => { console.log(ip); })` – Bergi Jun 26 '17 at 15:16
  • I know that..but i want to assing the ip to a variable so as to call a service – RamAlx Jun 26 '17 at 15:23
  • i send a request request: { name:name, lastname:lastname, ip_adress: (the_ip adress from the function) } – RamAlx Jun 26 '17 at 15:28
  • Yes. Do that from inside the callback. – Bergi Jun 26 '17 at 15:29
  • I'm writing ip_adress: clientsIpAdress(ip => ip). Didn't work – RamAlx Jun 26 '17 at 16:00
  • Of course it didn't. `clientsIpAddress(ip => { request({name, lastname, ip_address: ip}); });`! – Bergi Jun 26 '17 at 17:23
  • Why didn't you post it as an answer so as to upvote it? :) – RamAlx Jun 27 '17 at 05:17
  • Because the duplicate and all the other comments explained exactly that already. – Bergi Jun 27 '17 at 09:27

0 Answers0