To monitor a URL

1. In your project's primary HTML file, include the Shockwave file (Script 13.1): <script src="servicemonitor.swf" ^ type="application/x-shockwave-^ flash"></script> To use the URLMonitor class, an application must include this file. For the type attribute, make sure you specify its value as application/x-shockwave-flash.

2. In a JavaScript block, create the two objects: var url = new air.URLRequest ^('http://www.example.com'); var monitor = new air.URLMonitor(url);

This code is pretty much the same as the code explained earlier. The URL http://www.example.com will work (it does actually exist), or you can use any other address here.

continues on next page

Script 13.1 This first example confirms the application's ability to access a particular Web site.

3 <title>URLRequest and URLMonitor</title>

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

5 <script type="text/javascript" src="AIRAliases.js"></script>

6 <script type="text/javascript">

8 // Create the two objects:

9 var url = new air.URLRequest('http://www.example.com'); —|

10 var monitor = new air.URLMonitor(url); jjj

11 c

13 monitor.addEventListener(air.StatusEvent.STATUS, statusChange);

16 monitor. startO; C

17 5

18 // Function called whenever the monitor status changes. H

19 function statusChange(e) { O

21 // Find the document element: V)

22 var status = document.getElementById('status');

24 // Set the value based upon availability:

25 if (monitor.available) {

26 status.innerText = 'available';

28 status.innerText = 'not available';

37 <div>The resource is <strong id="status"></strong>.</div>

3. Add an event listener to the URLMonitor object: monitor.addEventListener(air.

— StatusEvent.STATUS, statusChange); When the URLMonitor object's status changes—from available/connected to unavailable/unconnected or vice versa, the statusChangeO function will be called.

4. Start the monitor: monitor.startO;

If you don't include this line, the application won't actually do anything.

5. Begin the statusChangeO function: function statusChange(e) {

var status = document.getElementBy - Id('status');

The role of this function is to add some text to the application indicating whether the given resource is available or not. The specific document element that will be updated is called status, and a reference to that is created here.

O O O URLRequest and URLMonitor

The resource is available

Figure 13.1 If the application can access the URL, this will be the result.

6. Complete the statusChangeO function:

if (monitor.available) {

status.innerText = 'available'; } else {

} // End of statusChangeO function. If the available property of the monitor object has a value of true, the status element will be assigned an innerText value of available. Otherwise it's assigned a value of not available.

7. Within the body of the page, create an area with an id of status:

<div>The resource is <strong id="status"></strong>.</div> Thanks to the conditional in the statusChangeO function, this sentence will end up reading either The resource is available. (Figure 13.1) or The resource is not available. (Figure 13.2).

QOO URLReqnest and URLMonitor

The resource is not available.

Figure 13.2 When the application cannot access the URL, either because the user's computer or the URL's server is not online, this will be the result.

8. Copy the servicemonitor.swf to the project directory.

You'll find it in the AIR SDK's frameworks folder.

9. Save, test, debug, and run the completed application.

Remember to include the Shockwave file when you build the actual .air file. As a quick way to test the application, while it's running, temporarily disable or enable your Internet connection.

0 0

Post a comment

  • Receive news updates via email from this site