Your application needs to monitor and determine whether a specific HTTP server can be reached.
Use the URLMonitor class to detect network state changes in HTTP/S endpoints.
Service monitor classes work through event notification and subsequent polling of the designated endpoint. Service monitoring is not an integrated function of Adobe AIR directly, and needs to be added before it can be used.
The classes for service monitoring are contained in the servicemonitor.swf file, which you can find in the frameworks directory of the Adobe AIR SDK. You should copy this file into the application project folder; you can include it through the use of the HTML SCRIPT tag. You also need to include the servicemonitor.swf file in the packaged Adobe AIR application. The SCRIPT tag used to include service monitoring functionality must come before the AIRAliases.js file is declared. You also must specify the content type on the SCRIPT tag as application/x-shockwave-flash:
<script src="servicemonitor.swf" type="application/x-shockwave-flash"></script> <script src="airaliases.js" type="text/javascript"></script>
The URLMonitor class takes a single argument in the constructor, an instance of the URLRequest class. The URLRequest constructor takes a String that represents the URL service endpoint to query. The URLRequest class also contains information about how to query the endpoint (i.e., GET, POST), and any additional data that should be passed to the server:
var request = air.URLRequest( 'http://www.adobe.com' ) ; var monitor = new air.URLMonitor( request );
The URLMonitor class will raise a StatusEvent.STATUS event when the network status changes. Once the event handler has been registered, the URLMonitor instance can be told to start watching for network start changes:
monitor.addEventListener( air.StatusEvent.STATUS, doStatus ); monitor.start();
After a network change has been propagated as an event, you can use the URLMonitor.available property on the originating URLMonitor instance to check for the presence of a connection. The URLMonitor.available property returns a Boolean value that reflects the state of the network. As it is necessary to query the originating URLMonitor instance for network availability, you should declare the object in a scope that is accessible across the application:
<html>
<head>
<title>Connectivity to an HTTP Server</title>
<style type="text/css">
body {
font-family: Verdana, Helvetica, Arial, sans-serif;
font-size: 11px;
color: #0B333C;
}
</style>
<script src="servicemonitor.swf"
type="application/x-shockwave-flash"></script>
<script type="text/javascript"
src="airaliases.js"></script>
<script type="text/javascript">
var monitor = null;
function doLoad()
{
var request = new air.URLRequest
( 'http://www.adobe.com' );
monitor = new air.URLMonitor( request );
monitor.addEventListener( air.StatusEvent.STATUS,
doStatus );
monitor.start();
}
function doStatus( e )
{
var elem = document.createElement( 'div' );
elem.innerText = monitor.available;
document.body.appendChild( elem );
}
</script>
</head>
<body onLoad="doLoad();">
</body>
</html>
A Jabber chat client is required to reflect network presence in the user interface, but the endpoint is a Jabber server on a specific port, and not HTTP/S.
Use the SocketMonitor class to detect network state changes against TCP/IP socket endpoints.
The service monitoring features are not built into Adobe AIR directly, and need to be added before they can be used. The servicemonitor.swf file, which is included in the Adobe AIR SDK, must be imported as an application resource and included via an HTML SCRIPT tag. The content type on the SCRIPT tag must be specified, and the SCRIPT tag for the service monitor classes must come before the AIRAliases.js SCRIPT tag.
<script src="servicemonitor.swf" type="application/x-shockwave-flash"></script> <script src="airaliases.js" type="text/javascript"></script>
The SocketMonitor class takes two arguments in the constructor: a String that represents the host endpoint, and a port on which the server is listening:
var host = 'im.mydomain.com'; var port = 5220; var monitor = new air.SocketMonitor( host, port );
The SocketMonitor class will raise a StatusEvent.STATUS event when the network status changes. Once the event handler has been registered, calling the SocketMonitor.start() method will start watching the network for changes:
monitor.addEventListener( air.StatusEvent.STATUS, doStatus ); monitor.start();
After a network change has been propagated as an event, you can use the SocketMonitor.available property on the originating SocketMonitor instance to check for the presence of a connection. The SocketMonitor.available property returns a Boolean value that reflects the state of the network. As a best practice, you should declare the SocketMonitor object in a scope that is accessible across the application and is referenced directly during event handling:
<html>
<head>
<title>Connectivity to a Jabber Server</title>
<style type="text/css">
body {
font-family: Verdana, Helvetica, Arial, sans-serif;
font-size: 11px;
color: #0B333C;
}
</style>
<script src="servicemonitor.swf"
type="application/x-shockwave-flash"></script>
<script type="text/javascript" src="airaliases.js"></script>
<script type="text/javascript">
var monitor = null;
function doLoad()
{
monitor = new air.SocketMonitor
( 'im.mydomain.com', 1234 );
monitor.addEventListener
( air.StatusEvent.STATUS, doStatus );
monitor.start();
}
function doStatus( e )
{
var elem = document.createElement( 'div' );
elem.innerText = monitor.available;
document.body.appendChild( elem );
}
</script>
</head>
<body onLoad="doLoad();">
</body>
</html>