Calendar

October 2007
S M T W T F S
« Sep   Nov »
 123456
78910111213
14151617181920
21222324252627
28293031  

Tag Cloud

Categories

Archives

Recent Posts

Recent Comments


« AIR Update Manager | Main | AIR Derby Winners »

AIR ConnectionManager

By Rich Tretola | October 1, 2007
4,524 views

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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);
        }
    }
}

Topics: Adobe AIR | 4 Comments »

4 Responses to “AIR ConnectionManager”

  1. Tim Says:
    October 1st, 2007 at 8:33 am

    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.

    Reply to this comment

  2. everythingflex Says:
    October 1st, 2007 at 10:32 am

    Whoops, I just updated the ConnectionManager :-)

    Reply to this comment

  3. ConnectionManager and UpdateManager for Beta 3 | EverythingFlex Says:
    April 14th, 2008 at 5:27 am

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

  4. Flex 3: Detecting Network Status | Imbimp.com Says:
    August 25th, 2009 at 9:25 am

    [...] are a number of methods to detect network connectivity, all of which can be read about here, here, here and here. What I found is that these seem to work fine when there is an actual change in your [...]

Comments