-1

I'm a little lost as to how to make the app wait until the client has network connectivity. The consumers of the app are not expected to start the app manually (they will not be very computer literate) and the app will start up automatically on a user login to the Windows/Linux/Mac machine. In such a case, how do I ensure that during the user's session on the computer, the app starts as soon as internet connectivity is available. One option I am using is the node-main parameter provided to run a script before launch. My code for the script is :-

isOnline = require('is-online');
online = false;
window.console.log("Hello");
var a = function () {
  while(online == false) {
    isOnline(function(err,_online) {
      online = _online;
    });
  }
};
a();

Hello gets logged but then my app starts loading and fails as expected due to the lack of internet connectivity. Any other ideas to implement this ?

bl_ur
  • 1
  • 1
  • 6
  • Interesting problem. How about promises? You can also probably run a loop that sends a get request from a site on intervals, if the get request returns a 200 status, then run the main function. – Brian Sep 21 '16 at 15:28
  • Unfortunately I cannot control when the node webkit app starts, just the script that runs before it. My only option seems to be - making my script run until I can ensure that the machine is connected to the internet. – bl_ur Sep 22 '16 at 09:32

3 Answers3

1

My previous answer wouldn't work. Here's one that should, it checks for Internet connectivity every 5 seconds and only exits when there's a connection.

var isOnline = require('is-online');
process.stdout.write("Hello\n");
var check = function() {
    process.stdout.write("Checking...\n");
    isOnline(function(err, _online) {
        if (_online) {
            // If you want to invoke a script directly, do so here.
            // If you want to script to exit when there's a connection,
            // just don't do anything here.
            process.stdout.write("Online\n");
        } else {
            process.stdout.write("Offline\n");
            setTimeout(check, 5000);
       }
   })
}
check();
  • I liked your approach. You can also use immediately invoked function expression here, by defining check as `(function check(){ ... })();`. So you wouldn't need to call it at the and again. – pegasuspect Oct 01 '19 at 16:36
0

You can use the navigator object to check the online status:

alert(navigator.onLine);

It returns a boolean so you can easily use logic like this...

if(navigator.onLine){Proceed();} else {Whatever();}

For a quick test, if you pull out your CAT5 cable from your computer you should get a false.

Usr29
  • 41
  • 1
0

You might also wanna use an async-await syntax with IIFE here:

;(async function onlineCheck() {
    if(await isOnline())
        run() // I enclosed all my code into a function named run.
    else {
        console.error("Retrying... Connection attempt falied at " + (new Date()).toString());
        await onlineCheck();
    }
})();
pegasuspect
  • 991
  • 4
  • 15