Hi all,

    This is my code for detect network status ,  it maybe help to
you.
    Thanks  Dimitri Glazkov (
http://code.google.com/p/glazkov-attic/source/browse/trunk/YourTimesheets/Scripts/monitor.js)
 
----------------------------------------------------------------------------------------------------
     /**
* The ParentWorkerPool of Monitor  whether the system is online or
offline
*
* Alex([email protected])
* 2009.3
*/
isOnlie: function(){
        workerPool = google.gears.factory.create('beta.workerpool');

        workerPool.onmessage = function(a, b, message) {
            if (message.sender == monitorchildWorkerId) {
                    if(message.text == 'online'){
                        $('#is-connected').show();
                        $('#is-disconnected').hide();
                    }else if(message.text == 'offline'){
                        $('#is-disconnected').show();
                        $('#is-connected').hide();
                    }
            }
        };

        var monitorchildWorkerId = workerPool.createWorkerFromUrl('/
javascripts/js_app/models/monitor.js');
        workerPool.sendMessage(window.location + '?monitor',
monitorchildWorkerId);

}

/**
* The ChinldWorkerPool of Monitor  whether the system is online or
offline
*
* Alex([email protected])
* 2009.3
*/

var POLLING_INTERVAL = 2000;

var wp = google.gears.workerPool;
var url;
var parentId;

var first = true;
var online;

var request = google.gears.factory.create('beta.httprequest', '1.0');
var timer = google.gears.factory.create('beta.timer');

var wp = google.gears.workerPool;

monitor = function (message) {
  var count = 0;
  var db = google.gears.factory.create('beta.database');
  db.open('red-CustomersManagement');
  request.open('HEAD', url + String(Math.floor(Math.random()*10000)));
  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      try {
        if (request.status == 200) {
          rs = db.execute("select * from isonline",[]);
          if(rs.isValidRow()){count = rs.field(0)};
          if (!online) {
            online = true;
            if (count == 0){
                db.execute("insert into isonline (state) values (?)",
['online']);
            }else{
                db.execute('update isonline set state=? where
state=?;', ['online',"offline"]);
            }

            rs.close();
            wp.sendMessage("online", parentId);
          }
        }
      }
      catch(e) {
        if (online || first) {
            online = false;
            first = false;
          db.execute('update isonline set state=? where state=?;',
['offline',"online"]);
          wp.sendMessage("offline", parentId);

        }
      }
      db.close();
      // wp.sendMessage('ddd', parentId);
      timer.setTimeout(monitor, POLLING_INTERVAL);
    }
  }
  try {
    request.send();
  }
  catch(e) {
    if (online) {
      online = false;
    var db = google.gears.factory.create('beta.database');
    db.open('red-CustomersManagement');
    db.execute('update isonline set state=? where state=?;',
['offline',"online"]);
    db.close();
      wp.sendMessage("offline", parentId);
    }
  }
}

wp.onmessage = function(a, b, message) {
  url = message.text;
  parentId = message.sender;
  monitor(message)
}

------------------------------------------------------------------------------------------------

thank you!

Alex

Reply via email to