Using the second option as a guide, you need to modify the virtual_pet application to check periodically for a new version. You also need to establish some way to distribute the current version number to eager clients.
In this example, you will use a ColdFusion component (CFC) to distribute the version number and a class called SimpleVersionManager to facilitate the periodic polling.
First, look at versionCheck.cfc, located in the download file available for this article. Copy it to a folder on your web server where it can be freely accessed. Take a look at the following code:
<cfcomponent>
<cffunction name="getVersion" returntype="numeric" output="false" access="remote">
<cfreturn 1>
</cffunction>
</cfcomponent>
The CFC defines a single function, getVersion. For the moment, getVersion simply returns a static version number when queried.
Now take a look at SimpleVersionManager.as, also located in the ZIP file:
//Tell flex we intend to broadcast two events
[Event("checkVersion")]
[Event("updateVersion")]
class SimpleVersionManager extends Object {
private var _pollTime:Number;
private var _applicationVersionNumber:Number;
private var _latestVersionNumber:Number;
private var pollInterval:Number;
public var addEventListener : Function;
public var removeEventListener : Function;
private var dispatchEvent : Function;
/* The requestVersionCheck function is called periodically
via the SetInterval. It dispatches an event asking for
the latest version of the software available */
private function requestVersionCheck() {
//Cancel our current interval.
//We will wait for a response before we ask again
clearInterval( pollInterval );
//We need to find out the current version of this software
this.dispatchEvent({type:"checkVersion",target:this});
}
/*getter/setter functions for the version number
of this application */
public function set applicationVersionNumber( value:Number ):Void {
//This is the version number of our local application
_applicationVersionNumber = value;
}
public function get applicationVersionNumber():Number {
return _applicationVersionNumber;
}
/* getter/setter functions for the latest version number of
this application according to an outside authority */
public function set latestVersionNumber( value:Number ):Void {
//This is the latest available version number of the application
_latestVersionNumber = value;
//We check if we are up to date
if ( applicationVersionNumber < latestVersionNumber ) {
//We need to update. We inform whomever may be listening
this.dispatchEvent({type:"updateVersion",target:this});
} else {
//We have a current version for the moment.
//We will check again later
pollInterval = setInterval( this, "requestVersionCheck", pollTime );
}
}
public function get latestVersionNumber():Number {
return _latestVersionNumber;
}
//getter/setter functions for our polling interval
public function set pollTime( value:Number ):Void {
if ( value < 1000 ) {
/*For our purposes, we are going to constrain the poll
period so that it cannot be less than a second
in practice, this should be much, much longer
*/
value = 1000;
}
_pollTime = value;
}
public function get pollTime():Number {
return _pollTime;
}
/*We call this method when we are ready to begin
managing the version of our application */
public function manageVersion() {
/*immediately request a version check on startup.
we want to find out if a new version exists before
the user gets to far into using the application */
requestVersionCheck();
}
function SimpleVersionManager() {
//Initialize the event broadcaster
mx.events.EventDispatcher.initialize(this);
}
}
The SimpleVersionManager class performs a simple task. It maintains the current application version and broadcasts an event, periodically asking the application to check for a new version. When the application receives the new version, SimpleVersionManager compares the versions and broadcasts an event, letting the application know if an update is available.
Look at virtual_pet2, located in sample download, to see how SimpleVersionManager is integrated:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" xmlns="*" creationComplete="initialze( event );">
<mx:Script>
<![CDATA[
//For the moment we have statically set a version
//number in our application
var ourVersion:Number = 1;
function queryServerForVersion( event ) {
/* This function is called when SimpleVersionManager
broadcasts a checkVersion event */
versionService.getVersion();
}
function getNewVersion( event ) {
mx.core.Application.alert( "Your version is outdated. You have version " + versionManager.applicationVersionNumber + " and the server has " + versionManager.latestVersionNumber );
/*Right now we have left this commented out
However, the line below will redirect the browser to
the url of your application */
//getURL( "http://yourserver/virtual_pet2.mxml?version=" );
}
function setLatestVersion( event ) {
/*This is called when we successfully retrieve the version number
from the server. Sets the latestVersion in the SimpleVersion Manager*/
versionManager.latestVersionNumber = event.result
}
function initialze( event ) {
/* This is caled by the Application creationComplete event.
After everything is created, we start actively managing the version */
versionManager.manageVersion();
}
]]>
</mx:Script>
<!--We setup a Remote Object to call our CFC on a ColdFusion Server-->
<mx:RemoteObject id="versionService"
endpoint="{yourServer}/flashservices/gateway?"
source="versionCheck"
showBusyCursor="false">
<!--We are going to call the getVersion function in the cfc and
return the results to setLatestVersion-->
<mx:method name="getVersion"
result="setLatestVersion(event );"
fault="mx.core.Application.alert('Problem Getting Version')"/>
</mx:RemoteObject>
<!--Instantiate a SimpleVersion Manager
We pass it our applicationVersionNumber
tell it we wish to check for a new version every 30 seconds
(This is of course extreme, but it is an example)
and tell it to call queryServerForVersion when a checkVersion event occurs
and to call getNewVersion when an update version event occurs-->
<SimpleVersionManager id="versionManager"
applicationVersionNumber="{ourVersion}"
pollTime="30000"
checkVersion="queryServerForVersion( event );"
updateVersion="getNewVersion( event );"/>
<!--Spike-->
<Dog id="myPet"
title="Virtual Pet RIA"
petName="Spike"/>
</mx:Application>
Copy virtual_pet2.mxml, SimpleVersionManager.as, Dog.mxml, and Dog.png to your Flex server. Ensure that you have copied versionCheck.cfc to your ColdFusion server.
Change the endpoint of the RemoteObject tag in virtual_pet2.mxml to reflect the address of your ColdFusion server. If you point your web browser to virtual_pet2.mxml on your Flex server, you should see identical output to the previous version.
However, if you open versionCheck.cfc and change the following line:
<cfreturn 1>
to this line:
<cfreturn 2.2>
Any client running virtual_pet2.mxml will display the alert box shown in Figure 2.
Figure 2. Alert box announcing that Spike is an old version
In virtual_pet2.mxml, there is the following line of code:
//getURL( "http://yourserver/virtual_pet2.mxml?version=" );
Currently this line is commented out. You could use this line to redirect the client to a download page or an updated version of the application. Normally this would be the same URL from which the user originally downloaded the client.
Any client running virtual_pet2.mxml can now be updated automatically if you change the version number of the CFC located on the ColdFusion server.