Monitoring network connectivity



Adobe® AIR™ provides the means to check for changes to the network connectivity of the computer on which an AIR application is installed. This information is useful if an application uses data obtained from the network. Also, an application can check the availability of a network service.

Detecting network connectivity changes

Your AIR application can run in environments with uncertain and changing network connectivity. To help an application manage connections to online resources, Adobe AIR sends a network change event whenever a network connection becomes available or unavailable. The application’s NativeApplication object dispatches the network change event. To react to this event, add a listener:

air.NativeApplication.nativeApplication.addEventListener(air.Event.NETWORK_CHANGE, onNetworkChange);

And define an event handler function:

function onNetworkChange(event) 
{ 
    //Check resource availability 
}

The Event.NETWORK_CHANGE event does not indicate a change in all network activity, but only that a network connection has changed. AIR does not attempt to interpret the meaning of the network change. A networked computer may have many real and virtual connections, so losing a connection does not necessarily mean losing a resource. On the other hand, new connections do not guarantee improved resource availability, either. Sometimes a new connection can even block access to resources previously available (for example, when connecting to a VPN).

In general, the only way for an application to determine whether it can connect to a remote resource is to try it. To this end, the service monitoring frameworks in the air.net package provide AIR applications with an event-based means of responding to changes in network connectivity to a specified host.

Note: The service monitoring framework detects whether a server responds acceptably to a request. This does not guarantee full connectivity. Scalable web services often use caching and load-balancing appliances to redirect traffic to a cluster of web servers. In this situation, service providers only provide a partial diagnosis of network connectivity.

Service monitoring basics

The service monitor framework functions separately from the AIR framework. For HTML-based AIR applications, the servicemonitor.swf must be included in your AIR application package. The servicemonitor.swf must also be included in your AIR application code, as follows:

<script source="servicemonitor.swf" type="application/x-shockwave-flash"/>

The servicemonitor.swf file is included in the frameworks directory of the AIR SDK.

The ServiceMonitor class implements the framework for monitoring network services and provides a base functionality for service monitors. By default, an instance of the ServiceMonitor class dispatches events regarding network connectivity. The ServiceMonitor object dispatches these events when the instance is created and whenever a network change is detected by Adobe AIR. Additionally, you can set the pollInterval property of a ServiceMonitor instance to check connectivity at a specified interval in milliseconds, regardless of general network connectivity events. A ServiceMonitor object does not check network connectivity until the start() method is called.

The URLMonitor class, a subclass of the ServiceMonitor class, detects changes in HTTP connectivity for a specified URLRequest.

The SocketMonitor class, also a subclass of the ServiceMonitor class, detects changes in connectivity to a specified host at a specified port.

Detecting HTTP connectivity

The URLMonitor class determines if HTTP requests can be made to a specified address at port 80 (the typical port for HTTP communication). The following code uses an instance of the URLMonitor class to detect connectivity changes to the Adobe website:

<script src="servicemonitor.swf" type="application/x-shockwave-flash" /> 
 
<script> 
    var monitor; 
    function test() 
    { 
        monitor = new air.URLMonitor(new air.URLRequest('http://www.adobe.com')); 
        monitor.addEventListener(air.StatusEvent.STATUS, announceStatus); 
        monitor.start(); 
    } 
    function announceStatus(e) { 
        air.trace("Status change. Current status: " + monitor.available); 
    } 
</script>

Detecting socket connectivity

AIR applications can also use socket connections for push-model connectivity. Firewalls and network routers typically restrict network communication on unauthorized ports for security reasons. For this reason, developers must consider that users may not have the capability of making socket connections.

Similar to the URLMonitor example, the following code uses an instance of the SocketMonitor class to detect connectivity changes to a socket connection at 6667, a common port for IRC:

<script src="servicemonitor.swf" type="application/x-shockwave-flash" /> 
 
<script> 
    function test() 
    { 
        socketMonitor = new air.SocketMonitor('www.adobe.com', 6667); 
        socketMonitor.addEventListener(air.StatusEvent.STATUS, socketStatusChange); 
        socketMonitor.start(); 
    } 
    function announceStatus(e) { 
        air.trace("Status change. Current status: " + socketMonitor.available); 
    } 
</script>