AIR Tip 1 – Monitoring Your Internet Connection

December 15th, 2007

AIR is meant to facilitate applications that work when online and offline. For example, if you developed an AIR application to manage your blog, you would want to be able to write blog posts whether you were online or offline. To accomplish this, the application would do one of two actions depending on whether it had an internet connection. If it were online, it would take your post and upload it. If it weren't online, it would store it (either in a file or in a local SQLite database).

VERSION: This tutorial is current for AIR Beta 3.

Today, you are going to build a very simple AIR application. If will be a window that will have three main items: a search box, a submit button, and an image that indicates if you are connected to the Internet. If a user types something into the text input and hits the search button, it will open up your browser and search for that term on Google. However, if you are not connected to the Internet, the "search" button will be disabled.

AIR has two specific functions for monitoring your internet connection, URLMonitor [ Flex | Javascript ] and SocketMonitor [ Flex | Javascript ]. These classes both implement the ServiceMonitor Class [ Flex | Javascript ]. To monitor your connection you just follow the steps below:

  1. Create a URLRequest with the URL that you want to monitor. You can set its mode to "HEAD" to avoid getting the entire page every time.
  2. Create a new URLMonitor and assign it the URL that you want it to monitor.
  3. Add a new Event Listener for the URLMonitor that listens for the StatusEvent.STATUS event and create the function to act on that event.
  4. Set how often you want to the URLMonitor to run and start it.

If any of this seems confusing, don't worry, you will look at each item in the code below. Also, the full source code will be available for both examples.

Coding the Example

For the application, you are going to create a function that gets run when the application starts. This will be where you will create your URLRequest and URLMonitor objects. For Flex, this will be a function that responds to the CreationComplete event, for the HTML/Javascript example, this will be a function that responds to the "onload" event of the body tag.

Flex

Actionscript:
  1. import air.net.URLMonitor;
  2. import flash.net.navigateToURL;
  3. import flash.net.URLRequest;
  4.  
  5. // DEFINE The Variable that will hold the URLMonitor
  6. private var monitor:URLMonitor;
  7.  
  8. private function init():void {
  9.  
  10. // URLRequest that the Monitor Will Check
  11. var url:URLRequest = new URLRequest("http://www.davidtucker.net/index.php");
  12. // Checks Only the Headers - Not the Full Page
  13. url.method = "HEAD";
  14.  
  15. // Create the URL Monitor and Pass it the URLRequest
  16. monitor = new URLMonitor(url);
  17. // Set the Interval (in ms) - 3000 = 3 Seconds
  18. monitor.pollInterval = 3000;
  19. // Create the Event Listener that Will Be Fired When Connection Changes
  20. monitor.addEventListener(StatusEvent.STATUS,on_connection);
  21. // Start the URLMonitor
  22. monitor.start();
  23.  
  24. }

HTML/Javascript

JavaScript:
  1. var monitor;
  2.  
  3. function onLoad() {
  4.  
  5. // URLRequest that the Monitor Will Check
  6. var request = new air.URLRequest( "http://www.davidtucker.net/index.php" );
  7. // Checks Only the Headers - Not the Full Page
  8. request.method = "HEAD";
  9.  
  10. // Create the URL Monitor and Pass it the URLRequest
  11. monitor = new air.URLMonitor( request );
  12. // Create the Event Listener that Will Be Fired When Connection Changes
  13. monitor.addEventListener( air.StatusEvent.STATUS, doStatus );
  14. // Start the URLMonitor
  15. monitor.start();
  16.  
  17. }

Hopefully you can see that there is not a great deal of difference between the two. Calling an AIR function within Javascript is just as easy as it is in Flex.

NOTE: If you want this javascript to function properly, be sure to include the AIRAliases.js file and the servicemonitor.swf file in your application. If you do not, this code will not function properly. Your code can function without the AIRAliases.js - the method names are just longer. However, you cannot use the URLMonitor or SocketMonitor without the servicemonitor.swf file.

Flex Application
Source Code

HTML / Javascript Application
Source Code

AIR | Comments | Trackback | Del.icio.us | Digg | Technorati Jump to the top of this page

7 comments on “AIR Tip 1 – Monitoring Your Internet Connection”

  1. 01

    I am trying to run this app and I am getting this error
    SyntaxError: Parse error

    app-resource:/servicemonitor.swf : 1

    TypeError: Value (result of expression window.runtime.air.net.URLMonitor) is not a constructor. Cannot be used with new.

    onLoad at app-resource:/tip.html : 21

    onLoad at app-resource:/tip.html : 21

    onload at app-resource:/tip.html : 72

    Matt Graf at October 30th, 2007 around 2:46 pm
    Jump to the top of this page
  2. 02

    I haven’t updated this tutorial for AIR Bet 2 yet. I am not sure if this is the cause of that issue - but I plan to update it within the next day or two.

    David Tucker at October 31st, 2007 around 8:09 am
    Jump to the top of this page
  3. 03

    I don’t know if you are aware of it, but there’s an huge amount of spam links in your article source, hidden in a display: none bold tag….

    Ruben at January 15th, 2008 around 6:59 am
    Jump to the top of this page
  4. 04

    @Ruben - Thanks! I am still recovering from a minor hack a while back.

    David Tucker at January 15th, 2008 around 7:02 am
    Jump to the top of this page
  5. 05

    [...] AIR技巧 AIR Tip 1: Monitoring a Network Connection (AIR Beta 3) AIR Tip 2: Going Fullscreen (AIR Beta 3) AIR Tip 3: What Version is My Application [...]

    Jump to the top of this page
  6. 06

    [...] AIR Tip 1: Monitoring a Network Connection (AIR 1.0) [...]

    雨飞blog » David Tucker的air tips at March 5th, 2008 around 8:08 pm
    Jump to the top of this page
  7. 07

    [...] 原文在这里 [...]

    Jump to the top of this page

Leave a Reply

  •  
  •  
  •  

You can keep track of new comments to this post with the comments feed.

Badges

View David Tucker's profile on LinkedIn
Inside RIA Badge

Community Posts