0

I understand there may be duplicate threads on this so I apologise in advance, however, I cannot seem to either find the right thread to help me, or more so, get my example to work...

What I am trying to do

I am trying to execute JavaScript code in my AngularJS app that obtains my local IP address, and I want to get this IP address before anything else is executed.

I have placed this code in my app.run() module at the very beginning and then have other code directly under it like this example:

app.run(function() {

    function getIP() {
        var myIP = '';
        window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;//compatibility for Firefox and chrome
        var pc = new RTCPeerConnection({iceServers:[]}), 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)
        {
            if (ice && ice.candidate && ice.candidate.candidate)
            {
                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];
                pc.onicecandidate = noop;
                return myIP;
            }
        };

    }

    var ip = getIP();
    console.log(ip);

    console.log('Starting App');

    //Do some stuff

    console.log('Launching');

});

The result I get is:

  • undefined
  • Starting App
  • Launching

Obviously the code behind obtaining the IP Address takes time to execute and JavaScript is continuing to execute the remaining code in the module. However, I'm not sure how to overcome this correctly. I have tried using promises and callbacks and factories but doesn't seem to make much difference for me.

Am I doing something wrong? Do I need to put all remaining module code in a separate function maybe? I'm thinking it has something to do with the function inside the function in getIP()

Riples
  • 1,167
  • 2
  • 21
  • 54
  • `onicecandidate ` is not synchronous, it's asynchronous. I'd convert it to a `Promise` and then `await` it or call `.then` on it – CertainPerformance Feb 12 '19 at 06:48
  • Sorry @CertainPerformance but I am still struggling with this. I have added a `Promise` to `pc.onicecandidate = function(ice)` but I still can't get past the `undefined` variable. I don't suppose you could post an example, as I have tried to follow the duplicate post and I'm still not having any luck. – Riples Feb 12 '19 at 11:43
  • See the post How do I convert an existing callback API to Promises – CertainPerformance Feb 12 '19 at 11:46
  • Seems I still can't get my head around this, or something isn't working because I now get an IP address (rather than `undefined`) but after all other code has been executed. Doesn't matter, I'll try and get around it some other way - it's starting to do my head in! Thanks anyway for your help. – Riples Feb 12 '19 at 12:44

0 Answers0