Subscribe: Posts / Comments / Email


RSS

Adobe Air – Connection Manager

Wed, Oct 8, 2008

Air, Flex, Snippets

Here is a very useful script that I re-use and re-use when building Air applications. Its a simple script I am sure you can make out what is going on here.

ConnectionManager.as

package com.jonniespratley.air.managers
{
	import air.net.URLMonitor;

	import flash.events.StatusEvent;
	import flash.net.URLRequest;

	import mx.controls.Alert;

	public class ConnectionManager
	{
		private var statusEvent:StatusEvent;
		private var urlMonitor:URLMonitor;
		private var showAlert:Boolean;
		private var message:String;

		[Bindable] public var connectionURL:String;
		[Bindable] public var isConnected:Boolean;	

		public function ConnectionManager( showAlert:Boolean = true,
					connectionURL:String = "http://del.icio.us",
					message:String = "Del.icio.us Air must be connected to the internet to retrieve lastest bookmarks\nan Internet Connection" )

		{
			this.showAlert = showAlert;
			this.connectionURL = connectionURL;
			this.message = message;

			startMonitor();
		}

		public function startMonitor():void
		{
			var urlRequest:URLRequest = new URLRequest( connectionURL );
				urlRequest.method = "HEAD";

				urlMonitor = new URLMonitor( urlRequest );
				urlMonitor.addEventListener( StatusEvent.STATUS, statusChanged );
				urlMonitor.start();
		}

		public function statusChanged( event:StatusEvent ):void
		{
			this.isConnected = urlMonitor.available;

			if ( !this.isConnected && this.showAlert )
			{
				Alert.show( this.message, "Connection Failure" );
			}

			statusEvent = new StatusEvent( StatusEvent.STATUS );

			dispatchEvent( statusEvent );			

		}

	}
}

Tags: , ,

5 Comments For This Post

  1. funkybot Says:

    I’d go with something simpler, or just quicker to write :)

    var monit:URLMonitor;

    function init():void {
    var req:URLRequest = new URLRequest(“http://spreadingfunkyness.com”);
    req.method = URLRequestMethod.HEAD;
    monit = new URLMonitor(req);
    monit.addEventListener(StatusEvent.STATUS, change);
    monit.start();
    }

    function change(e:StatusEvent):void {
    trace(“status is “+monit.available);
    }

  2. Rick Winscot Says:

    Helpful post! Just an fyi – the URLRequest method as “HEAD” fails with a 405 MethodNotAllowed when querying against http://www.amazon.com. Additionally, the URLMonitor can be tricked to think that the endpoint is online if the individual uses a service like OpenDNS – which swallows the error and sends back a page of ‘recommendations’ and a status code of 200. So… rather than thinking that the URLMonitor is a means to determine that you are ‘online’ it really should be used to determine that the endpoints you use within the application are ‘available.’

  3. Jonnie Says:

    Thats good to know, I’ll watch for that next time I do anything with amazon. How should we go about doing this is a fault proof solution?

  4. Alexwebmaster Says:

    Hello webmaster
    I would like to share with you a link to your site
    write me here preonrelt@mail.ru

  5. John Ivens Says:

    I am a Pensioner. Not very computer literate.
    I don’t know how or why but Adobe Air tried to install on my laptop.
    I don’t recall downloading it.
    However, it says it is damaged, so download again.
    It always says the same thing.
    I have no idea what Air does, and doubt I need it,since I have never had the chance to use it.
    Contact Adobe?
    You must be joking. After 30 minutes trying I go to lie down with an ice bag on my forehead.
    “OK, let’s get rid of the bloody thing”.
    You think life is that simple?
    It won’t Delete and has now jammed my Add and Remove Page.
    Hopefuly it will disappear when I close down.
    For sure, I will never download another Adobe product in the future.
    I can’t even tell Adobe this. They live in a world of their own.
    I don’t appear to be the first who has had problems like this.

Leave a Reply