[gears-users] Online/Offline Detection

7 views
Skip to first unread message

Gopal kalasa

unread,
May 17, 2010, 11:00:43 AM5/17/10
to Gears Users, aswath.s...@gmail.com
HI,

I want to use google gears to work offline with gwt module,
i am planing to write two EntryPoints in one gwt module, one for each
offline and online.
but failing to find out offline/online detection.

Is there any function in Gears to check whether the application is
online or offline?
Can you provide code for offline/online detection?



Thank You in Advance
Gopal

karim fathallah

unread,
May 18, 2010, 4:38:04 AM5/18/10
to gears...@googlegroups.com
hi, this function test if your server is joignable or not :

<div id="serverStatus">SERVER</div>
<div id="pings">TIME</div>
<SCRIPT>
  var numPings = 0;
  var request = google.gears.factory.create('beta.httprequest');
  var TIME_BETWEEN_PINGS = 3*1000;
  var PING_TIMEOUT_SECONDS = 1*1000;
  function pingSuccess() {
    if(request.responseText != "" && request.responseText.indexOf("404 Page not found") == -1){
      document.getElementById('serverStatus').innerHTML = "[Server Accessible]";
    } else {   
      document.getElementById('serverStatus').innerHTML = "[Server Inaccessible]"; 
    }
  }
 function isServerAvailable() { 
   var resource_to_test = "http://localhost:8080/ZZenfance/usager.jsp";
    resource_to_test + "?q=" + Math.floor(Math.random() * 100000); 
    request.open('Get',resource_to_test);
    window.setTimeout("pingSuccess()",PING_TIMEOUT_SECONDS); 
    request.onreadystatechange = function() {   
      if (request.readyState == 4) {
        numPings++;
        document.getElementById('pings').innerHTML = "Number of pings: " + numPings;
      }
    };
    request.send();
    window.setTimeout("isServerAvailable()",TIME_BETWEEN_PINGS);
  }
  isServerAvailable();
</SCRIPT>

And this function show that are you connected or not :

// Test if the browser is connected to the Internet
function online(){
     return navigator.onLine;
}

Eduard Martini

unread,
May 18, 2010, 5:00:28 AM5/18/10
to Gears Users
There is no clear definition of online/offline state.

You can have internet access (online) but ou want to use the
application in offline mode (from different reasons like expensive/bad
internet connection, speed, leaving soon and do not want to sync, etc)

Also you may want to change to online mode but not having internet
connection, then the offline mode will be forced.

So, you will need an "internet access check". This can be done with an
AJAX request to a resource on your website. And the Gears app state
should be stored inside the application.
Reply all
Reply to author
Forward
0 new messages