Network Detection for Gears

2 views
Skip to first unread message

dfalck

unread,
Feb 1, 2008, 3:05:04 PM2/1/08
to Google Gears
I found this to be a really good tutorial and source of ideas/
inspiration for detecting your network when going offline:
http://positionabsolute.net/blog/2007/09/google-gears-offline-detection.php

하대환

unread,
Feb 2, 2008, 7:31:15 AM2/2/08
to google...@googlegroups.com
Good post and blog.

here's mine.

================== is server available? ===============================

var Available = function(onCallBack, offCallBack) {
    this.onCB = onCallBack;
    this.offCB = offCallBack;

    this._worker = this.roll();
};

Available.prototype.roll = function() {
    return setInterval(this.bind(this.doCheck), "5000");
}

Available.prototype.regCallback = function(oncase, func) {
    if(oncase.toLowerCase == "online") this.onCB = func;
    else this.offCB = func;
};

Available.prototype.discode = {
    0: true,
    500: true,
    502: true,
    503: true,
    509: true
};

Available.prototype._xhrCandidates = [
    function() { return new XMLHttpRequest(); },
    function() { return new ActiveXObject('Msxml2.XMLHTTP'); },
    function() { return new ActiveXObject('Microsoft.XMLHTTP'); },
    function() { return new ActiveXObject('Msxml2.XMLHTTP.4.0'); },
];

Available.prototype.xhr = function() {
    if(!this.__xhr) {
        find_XMLHTTP:
        for(var i=0; i<this._xhrCandidates.length; i++) {
            try {
                this.__xhr = this._xhrCandidates[i]();
                break find_XMLHTTP;
            }
            catch(e) {
            }
        }
    }

    return this.__xhr;
};

Available.prototype.doCheck = function() {
    try {
        var url = location.protocol + "//" + location.hostname + "/" + new Date().getTime();

        this.xhr().open('GET', url, false);
        this.xhr().send(null);

        var callback = (this.xhr().status in this.discode) ? "offCB" : "onCB";
        if(callback) this[callback]();
    } catch(e) {
        if(this._worker) clearInterval(this._worker);
    }
};

Available.prototype.bind = function(func) {
    var scope = this;
    return function() {
        return func.apply(scope);
    }
};

//=========== use ================

new Available(
  function() { // when on
    alert("online")
  },
  function() { // when off
    alert("offline")
  }
);

2008/2/2, dfalck <david...@gmail.com>:

Dimitri Glazkov

unread,
Feb 2, 2008, 10:31:39 AM2/2/08
to google...@googlegroups.com

하대환

unread,
Feb 2, 2008, 11:51:15 AM2/2/08
to google...@googlegroups.com
nice and smooth =)

08. 2. 3, Dimitri Glazkov <dimitri...@gmail.com>님이 작성:

Aaron Boodman

unread,
Feb 2, 2008, 1:04:08 PM2/2/08
to google...@googlegroups.com
Oooh, I want to play. (untested, and if I had bind() I could make it shorter).

====

function monitorOnlineness(onlinenessChanged) {
var interval = 1000; // check network once a second
var onlineness = null;
var timer = google.gears.factory.create('beta.timer');
var timerId = null;

function check() {
timerId = timer.setTimeout(function() {
var req = google.gears.factory.create('beta.httprequest');
req.onload = function() { onlinenessChanged(true); }
req.onerror = function() { onlinenessChanged(false); }
}, interval);
}

function updateOnlineness(val) {
if (onlineness !== val) {
onlineness = val;
onlinenessChanged(val);
}
check();
}

function cancel() {
if (timerId) {
timer.clearTimeout(timerId);
}
}

return cancel;
}

// to use:
var cancel = monitorOnliness(function(isOnline) {
console.log(isOnline ? 'online!' : 'offline :(');
});

// to cancel:
cancel();

===


- a

Aaron Boodman

unread,
Feb 2, 2008, 1:07:30 PM2/2/08
to google...@googlegroups.com
On Sat, Feb 2, 2008 at 10:04 AM, Aaron Boodman <a...@google.com> wrote:
> untested

Argh, there needs to be an initial call to check() at the bottom of
monitorOnliness().

- a

Dimitri Glazkov

unread,
Feb 2, 2008, 2:22:30 PM2/2/08
to google...@googlegroups.com
I really like the the use of .onerror handler. That's nice.

:DG<

Aaron Boodman

unread,
Feb 2, 2008, 2:28:11 PM2/2/08
to google...@googlegroups.com
Too bad it will never get called because I never actually sent the
request :-). aa FAIL.

(there needs to be a url param to monitorOnliness() and an
req.open("GET", url) and req.send(null) in check())..

- a

Dimitri Glazkov

unread,
Feb 2, 2008, 2:42:49 PM2/2/08
to google...@googlegroups.com
It's ok, we got the idea..

Methink HEAD request is better, because server will know we're just
checking the pulse.

Aaron Boodman

unread,
Feb 2, 2008, 2:47:31 PM2/2/08
to google...@googlegroups.com
On Sat, Feb 2, 2008 at 11:42 AM, Dimitri Glazkov
<dimitri...@gmail.com> wrote:
> It's ok, we got the idea..
>
> Methink HEAD request is better, because server will know we're just
> checking the pulse.

Good point.

- a

하대환

unread,
Feb 2, 2008, 8:59:36 PM2/2/08
to google...@googlegroups.com
fun enuf, good enuf.

I'm on different timezone. (yes, has weak English too.) That's why I can't read fresh post when post still fresh.

Goal of my source that I posted is drop all dependences even gears. that's why i use window's timer instead of gears's one. (yes! dirty xhr finder, too)

The "onerror" is elegant way I think.

In fact, my project doesn't need real time network detection, (I'm enuf to check single request when action called) But very fun with GOOD sources.

Thanks Dimitri, Aaron.


Ah!

When I need real time detection, I want to post simplistic source with you guys ideas =)



2008/2/3, Aaron Boodman <a...@google.com>:
Reply all
Reply to author
Forward
0 new messages