Adobe Integrated Runtime (AIR) for JavaScript Developers Pocket Reference

(nextflipdebug5) #1

104 | Chapter 4: AIR Mini-Cookbook


var monitor =
new window.runtime.air.net.SocketMonitor( host, port );

TheSocketMonitorclass will raise aStatusEvent.STATUSevent
when the network state changes. Once the event handler has
been registered, calling theSocketMonitor.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, the
SocketMonitor.available property on the originating
SocketMonitorinstance can be used 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, the SocketMonitor object should be
declared in a scope that is accessible across the application
and referenced directly during event handling:


<html>
<head>

<title>Socket Monitor</title>
<script type="text/javascript" src="AIRAliases.js">
</script>
<script src="servicemonitor.swf"></script>

<script>
var monitor = null;

function doLoad( )
{
monitor = new window.runtime.air.net.Socket
Monitor( "im.mydomain.com", 5220 );
monitor.addEventListener( air.StatusEvent.STATUS, doStatus );
monitor.start( );
}

function doStatus( event )
{
var elem = document.createElement( "div" );

elem.innerText = monitor.available;
Free download pdf