Check whether your network connection is normal in flex or air

Source: Internet
Author: User

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 wocould want to be able to write blog posts whether you were online or offline. to accomplish this, the application wocould do one of two actions depending on whether it had an Internet connection. if it were online, it wocould take your post and upload it. if it weren't online, it wocould 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] andSocketmonitor[Flex | JavaScript]. These classes both implementServicemonitorClass [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 code:

Import air.net. urlmonitor;
Import flash.net. navigatetoURL;
Import flash.net. URLRequest;

// Define the variable that will hold the urlmonitor
Private var monitor: urlmonitor;

Private function Init (): void {

// URLRequest that the monitor will check
VaR URL: URLRequest = new URLRequest ("http://www.davidtucker.net/index.php ");
// Checks only the headers-not the full page
URL. method = "head ";

// Create the URL monitor and pass it the URLRequest
Monitor = new urlmonitor (URL );
// Set the interval (in MS)-3000 = 3 seconds
Monitor. Maid = 3000;
// Create the event listener that will be fired when connection changes
Monitor. addeventlistener (statusevent. Status, on_connection );
// Start the urlmonitor
Monitor. Start ();

}

JavaScript code:

VaR monitor;
 
Function onload (){
 
// URLRequest that the monitor will check
VaR request = new air. URLRequest ("http://www.davidtucker.net/index.php ");
// Checks only the headers-not the full page
Request. method = "head ";
 
// Create the URL monitor and pass it the URLRequest
Monitor = new air. urlmonitor (request );
// Create the event listener that will be fired when connection changes
Monitor. addeventlistener (air. statusevent. Status, dostatus );
// Start the urlmonitor
Monitor. Start ();
 
}

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.