Recent Widgets


Register for DashboardWidgets

Recent Forums Posts

Partners


iCompositions

MacDesktops.net

RSS Showcase
RSS Comments
RSS Forums

This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies. Posted in: Widget Development

Test if an internet connection is present

Author Message
andrethegiant



Joined: 08 May 2005
Posts: 78

Posted: Sat Aug 27, 2005 - 2:38 pm    Post subject: Test if an internet connection is present Reply with quote

Is this possible to do with javascript? And it should be somethin efficient, not trying to load a remote image and waiting 2 minutes.
View user's profile Send private message Send e-mail Widgets
gnome
Administrator


Joined: 26 Sep 2004
Posts: 145

Posted: Sat Aug 27, 2005 - 3:59 pm    Post subject: Reply with quote

You could request a page you can be relatively guaranteed to be up, i.e. Apple.com. You could also put up a page on your own site with no contents and request that (probably more efficient). Something sort of like this (UNTESTED):

Code:

var req;

function checkForInternet(thisURL) {
var hasInternet = -1; // -1 == checking, 0 is false, 1 is true
req = new XMLHttpRequest();
req.onreadystatechange = function() {
   if (req.readyState == 4) hasInternet = (req.status == 200)?1:0;
};
req.open("GET", thisURL, true); //USE ASYNC MODE HERE!!!
req.send("");
//the next line of code in this function should stall while waiting for a result.
//comment it out if it doesn't work (untested).
while(hasInternet == -1) {void(0);}
return (hasInternet == 1); //booleanify it for kicks, then return it.
}
View user's profile Send private message Widgets
maxintosh



Joined: 26 May 2005
Posts: 11
Location: east cost

Posted: Thu Oct 27, 2005 - 8:55 pm    Post subject: Reply with quote

I would actually put an "invisible" (1x1px transparent) gif on my site, try to load it. The images onError handler would tell you if there is no internet connection. Easier than loading a page.
View user's profile Send private message Send e-mail Visit poster's website AIM Address Widgets
seb2



Joined: 04 May 2005
Posts: 41

Posted: Fri Oct 28, 2005 - 1:45 am    Post subject: Reply with quote

Another idea: do a widget.system("/sbin/ping -c 1 www.apple.com", null) and have a look at the errorString afterwards. If it's longer than 1 character, you're likely not to be connected to the internet.
View user's profile Send private message Send e-mail Widgets
purvisa



Joined: 21 Jul 2005
Posts: 182
Location: Kirkland, WA

Posted: Fri Oct 28, 2005 - 11:59 pm    Post subject: Reply with quote

How about sending a ping to, say, 4.2.2.1? You can look at the terminal widgets to see how they get the ability to do that (just don't forget to use the proper plist settings).
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger Widgets
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.

 
Powered by phpBB © 2001, 2002 phpBB Group