air.net

# ServiceMonitor - AS3

Package | air.net
Class | public dynamic class ServiceMonitor
Inheritance | ServiceMonitor EventDispatcher Object
Subclasses | SocketMonitor, URLMonitor

Runtime Versions: | AIR 1.0

The ServiceMonitor class implements the framework for monitoring the status and availability of network services. The ServiceMonitor class acts as the base class for all other service monitors.

This class is included in the aircore.swc file. Adobe® Flash™ Builder™ loads this class automatically when you create a project for Adobe® AIR™. Adobe® Flex™ SDK also includes this aircore.swc file, which you should include when compiling the application if you are using Flex SDK.

In Adobe® Flash® CS3 Professional, this class is included in the ServiceMonitorShim.swc file. To use classes in the air.net package , you must first drag the ServiceMonitorShim component from the Components panel to the Library and then add the following `import` statement to your ActionScript 3.0 code:

    import air.net.*;

Copy

To use air.net package in Adobe® Flash® Professional (CS4 or higher):

1. Select the File > Publish Settings command.
2. In the Flash panel, click the Settings button for ActionScript 3.0. Select Library Path.
3. Click the Browse to SWC File button. Browse to Adobe Flash CSn/AIKn.n/frameworks/libs/air/aircore.swc file in the Adobe Flash Professional installation folder.
4. Click the OK button.
5. Add the following `import` statement to your ActionScript 3.0 code: `import air.net.*;`

More examples

Service monitoring

Public Properties
Hide Inherited Public Properties
Show Inherited Public Properties

| Property | Defined By
--- | --- | ---
|  | available : Boolean Whether the service is currently considered "available." The initial value is false until either a status check sets the property to true or the property is initialized to true explicitly. Typically, this property is set by the checkStatus() implementation in a subclass or specializer, but if the application has independent information about a service's availability (for example, a request just succeeded or failed), the property can be set explicitly. | ServiceMonitor
|  | constructor : Object A reference to the class object or constructor function for a given object instance. | Object
|  | lastStatusUpdate : Date [read-only] The time of the last status update. | ServiceMonitor
|  | pollInterval : Number The interval, in milliseconds, for polling the server. | ServiceMonitor
|  | running : Boolean [read-only] Whether the monitor has been started. | ServiceMonitor

Public Methods
Hide Inherited Public Methods
Show Inherited Public Methods

| Method | Defined By
--- | --- | ---
|  | ServiceMonitor() Creates a ServiceMonitor object. | ServiceMonitor
|  | addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void Registers an event listener object with an EventDispatcher object so that the listener receives notification of an event. | EventDispatcher
|  | dispatchEvent(event:Event):Boolean Dispatches an event into the event flow. | EventDispatcher
|  | hasEventListener(type:String):Boolean Checks whether the EventDispatcher object has any listeners registered for a specific type of event. | EventDispatcher
|  | hasOwnProperty(name:String):Boolean Indicates whether an object has a specified property defined. | Object
|  | isPrototypeOf(theClass:Object):Boolean Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter. | Object
|  | makeJavascriptSubclass(constructorFunction:Object):void [static] Adds public ServiceMonitor methods to a JavaScript constructor function's prototype. | ServiceMonitor
|  | propertyIsEnumerable(name:String):Boolean Indicates whether the specified property exists and is enumerable. | Object
|  | removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void Removes a listener from the EventDispatcher object. | EventDispatcher
|  | setPropertyIsEnumerable(name:String, isEnum:Boolean = true):void Sets the availability of a dynamic property for loop operations. | Object
|  | start():void Starts the service monitor. | ServiceMonitor
|  | stop():void Stops monitoring the service. | ServiceMonitor
|  | toLocaleString():String Returns the string representation of this object, formatted according to locale-specific conventions. | Object
|  | toString():String [override] Returns the string representation of the specified object. | ServiceMonitor
|  | valueOf():Object Returns the primitive value of the specified object. | Object
|  | willTrigger(type:String):Boolean Checks whether an event listener is registered with this EventDispatcher object or any of its ancestors for the specified event type. | EventDispatcher

Protected Methods

| Method | Defined By
--- | --- | ---
|  | checkStatus():void Checks the status of the service. | ServiceMonitor

Events
Click for more information on events
Hide Inherited Events
Show Inherited Events

| Event | Summary | Defined By
--- | --- | --- | ---
|  | activate | [broadcast event] Dispatched when the Flash Player or AIR application gains operating system focus and becomes active. | EventDispatcher
|  | deactivate | [broadcast event] Dispatched when the Flash Player or AIR application operating loses system focus and is becoming inactive. | EventDispatcher
|  | status | Indicates that the service status has changed. | ServiceMonitor

Property Detail

### available

property

`available:Boolean`

Runtime Versions: | AIR 1.0

Whether the service is currently considered "available."

The initial value is `false` until either a status check sets the property to `true` or the property is initialized to `true` explicitly.

Typically, this property is set by the `checkStatus()` implementation in a subclass or specializer, but if the application has independent information about a service's availability (for example, a request just succeeded or failed), the property can be set explicitly.

Implementation
`public function get available():Boolean`
`public function set available(value:Boolean):void`

### lastStatusUpdate

property

`lastStatusUpdate:Date` [read-only]

Runtime Versions: | AIR 1.0

The time of the last status update.

Implementation
`public function get lastStatusUpdate():Date`

### pollInterval

property

`pollInterval:Number`

Runtime Versions: | AIR 1.0

The interval, in milliseconds, for polling the server.

If zero, the server is not polled periodically, but only immediately after `start()` is called and when the network status changes.

The ServiceMonitor object only dispatches a `status` event if service status has changed (not on every poll interval). The object also dispatches a `status` event as a result of network connectivity changes (regardles of the poll interval).

The default value is `0.`

Implementation
`public function get pollInterval():Number`
`public function set pollInterval(value:Number):void`

### running

property

`running:Boolean` [read-only]

Runtime Versions: | AIR 1.0

Whether the monitor has been started.

Implementation
`public function get running():Boolean`
Constructor Detail

### ServiceMonitor

()
Constructor

`public function ServiceMonitor()`

Runtime Versions: | AIR 1.0

Creates a ServiceMonitor object.

This class is typically subclassed to monitor specific service types.

After creating a ServiceMonitor object (or a subclass object), call the `start()` method to begin monitoring the status of the service.

As with the Timer object, the caller should maintain a reference to the ServiceMonitor object. Otherwise, the runtime deletes the object and monitoring ends.

Method Detail

### checkStatus

()
method

`protected function checkStatus():void`

Runtime Versions: | AIR 1.0

Checks the status of the service.

A subclass override method for checking the status of the service.

Typically, this method will initiate a network operation whose completion or failure will result in setting the `available` property.

JavaScript code can specialize this method by defining a `checkStatus()` method in the "specializer" object.

### makeJavascriptSubclass

()
method

`public static function makeJavascriptSubclass(constructorFunction:Object):void`

Runtime Versions: | AIR 1.0

Adds public ServiceMonitor methods to a JavaScript constructor function's prototype.

Adds functions to the JavaScript constructor function's prototype that forward public ServiceMonitor functions to the ServiceMonitor object. This approximates a normal JavaScript subclass of the ActionScript base class.

A JavaScript class specializing a ServiceMonitor would look like this:

Copy

         // JavaScript Constructor function
         function MyHTTPMonitor(url, method)
         {
             // "that" variable makes "this" available in closures below
             var that = this;
             // Required initialization of the service monitor, returns the actual ServiceMonitor object.
             this.monitor = this.initServiceMonitor();
             // Initializes URLStream and event handlers.
             this._urlStream = new air.URLStream();
             this._urlRequest = new air.URLRequest(url);
             if (method)
             {
                 this._urlRequest.method = method;
             }
             else
             {
                 this._urlRequest.method = "GET";
             }
             function onStatus(event) {
                 that.monitor.available = Number(event.status) == 200;
                 that._urlStream.close();
             }
             function onError(event)
             {
                 that.monitor.available = false;
                 that._urlStream.close();
             }
             this._urlStream.addEventListener(air.HTTPStatusEvent.HTTP_RESPONSE_STATUS, onStatus);
             this._urlStream.addEventListener(air.SecurityErrorEvent.SECURITY_ERROR, onError);
             this._urlStream.addEventListener(air.IOErrorEvent.IO_ERROR, onError);
         }

         // Augment JavaScript prototype with public methods from ServiceMonitor
         air.ServiceMonitor.makeJavascriptSubclass(MyHTTPMonitor);

         // Implement specializer functions, just as you would when subclassing a JavaScript class
         MyHTTPMonitor.prototype.checkStatus = function()
         {
             air.trace('OVERRIDDEN checkStatus!', this);
             this._urlStream.load(this._urlRequest);
         }

To use the JavaScript class:

Copy

         var httpMon = new MyHTTPMonitor('http://www.adobe.com')

Be sure to load the AIRAliases.js and aircore.swf files with `script` tags.

Parameters

| `constructorFunction:Object` — The JavaScript object's `prototype` property. For example, if the JavaScript object that you are using to serve as a specializer object is named MyHTTPMonitor, pass `MyHTTPMonitor.prototype` as the value for this parameter.

### start

()
method

`public function start():void`

Runtime Versions: | AIR 1.0

Starts the service monitor.

### stop

()
method

`public function stop():void`

Runtime Versions: | AIR 1.0

Stops monitoring the service.

### toString

()
method

`override public function toString():String`

Runtime Versions: | AIR 1.0

Returns the string representation of the specified object.

Note: Methods of the Object class are dynamically created on Object's prototype. To redefine this method in a subclass of Object, do not use the `override` keyword. For example, a subclass of Object implements `function toString():String` instead of using an override of the base class.

Returns

| `String` — A string representation of the object.

Event Detail

### status

Event

Event Object Type: `flash.events.StatusEvent`
property StatusEvent.type = `flash.events.StatusEvent.STATUS`

Runtime Versions: | AIR 1.0

Indicates that the service status has changed.

The value of the `code` property is either `"Service.available"` or `"Service.unavailable"`, but best practice is to check the value of the `ServiceMonitor.available` property.

Defines the value of the `type` property of a `status` event object.

This event has the following properties:

Property | Value
--- | ---
`bubbles` | `false`
`cancelable` | `false`; there is no default behavior to cancel.
`code` | A description of the object's status.
`currentTarget` | The object that is actively processing the Event object with an event listener.
`level` | The category of the message, such as `"status"`, `"warning"` or `"error"`.
`target` | The object reporting its status.

© 2015 Adobe Systems Incorporated. All rights reserved.
Mon Apr 10 2017, 12:28 PM -07:00