• Home
  • About Me
  • AIR Central
  • AS3 Libs
  • Books
  • Flex Central
  • Resources
  • The Guru's
  •  

    AIR ConnectionManager

    UPDATED FOR AIR BETA 3

    Many AIR applications require Internet access. The problem is that since the application is launched from the desktop, an Internet connection can not be assumed. I have created a ConnectionManager that will handle all that is involved in testing for a connection.

    The constructor of the ConnectionManagr class accepts three optional arguments. The first is a Boolean which when true, will show the Alert message when a connection failure occurs, it defaults to true. The second is the URL that the ConnectionManager will attempt to connect to. It defaults to http://www.google.com. The last is a String that holds the alert message. It defaults to “This application requires an internet connection.”. The ConnectionManager has a public property named isConnected that will hold the current status of the application. To use the ConnectionManager you will simply need to create an instance of the ConnectionManager class and set any of the optional arguments. Once an instance is created, it will create an instance of the URLMonitor, which will announce any changes in status and set the isConnection property to true or false.

    <?xml version="1.0" encoding="utf-8"?>
    <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
        <mx:Script>
            <![CDATA[
                import com.everythingflex.air.managers.ConnectionManager;
                [Bindable]
                private var cm:ConnectionManager = new ConnectionManager();
            ]]>
        </mx:Script>
        <mx:Label text="Connection Status: {cm.isConnected}"
            fontSize="20"
            horizontalCenter="0"
            verticalCenter="0"/>
    </mx:WindowedApplication>

    cm1002.png

    Pull you internet connection and you will see that an Alert shows and the status property that is bound to the label component updates.

    cm1001.png

    And here is the ConnectionManager class:

    package com.everythingflex.air.managers
    {
        import air.net.URLMonitor;
        import flash.events.StatusEvent;
        import flash.net.URLRequest;
        import mx.controls.Alert;
       
        public class ConnectionManager
        {   
            private var eventObj:StatusEvent;
            private var urlMonitor:URLMonitor;
            // if true, show the Alert window
            private var showMessage:Boolean;
            // message to display when connection fails and showMessage is true
            private var message:String;
           
            // URL to test for a connection
            [Bindable]
            public var connectionURL:String;
            [Bindable]
            public var isConnected:Boolean = false;
           
            public function ConnectionManager(showMessage:Boolean=true,
                connectionURL:String="http://www.google.com",
                message:String="This application requires\nan Internet connection"):void{
               
                this.showMessage = showMessage;
                this.connectionURL = connectionURL;
                this.message = message;
                startMonitor();
            }
           
            // start the URLMonitor and test against the connectionURL
            public function startMonitor():void{
                var urlRequest:URLRequest = new URLRequest(connectionURL)
                urlRequest.method = "HEAD";
                urlMonitor = new URLMonitor(urlRequest);
                urlMonitor.addEventListener(StatusEvent.STATUS, statusChanged);
                urlMonitor.start();
            }
           
            // handle changes in the connection status and dispatches StatusEvent
            public function statusChanged(event:StatusEvent):void{
                this.isConnected =  urlMonitor.available;
                if(!this.isConnected && this.showMessage){
                    Alert.show(this.message, "Connection Failure");
                }
                eventObj = new StatusEvent(StatusEvent.STATUS);
                dispatchEvent(eventObj);
            }   
        }
    }

    3 Responses to “AIR ConnectionManager”

    1. Tim Says:

      There’s a class which does this in the AIR SDK.

      http://livedocs.adobe.com/labs/flex/3/langref/air/net/URLMonitor.html

      There’s a socket monitor as well.

    2. everythingflex Says:

      Whoops, I just updated the ConnectionManager :-)

    3. ConnectionManager and UpdateManager for Beta 3 | EverythingFlex Says:

      […] have updated the ConnectionManager and UpdateManager classes to work with AIR beta 3. I have also provided an example integrating both […]

    Leave a Reply