About server-side ActionScript



ColdFusion includes a module called the Flash Remoting service that acts as a broker for interactions between Flash and ColdFusion. Flash Remoting supports a range of object types, and lets you reference an ActionScript file that lives on a ColdFusion server. You can partition data-intensive operations on the server, while limiting the amount of network transactions necessary to get data from the server to the client.

Flash developers can create server-side ActionScript files to access ColdFusion resources; they do not have to learn CFML (ColdFusion Markup Language). This ability lets you logically separate the Flash presentation elements of your applications from the business logic. You have the option of creating ActionScript files that reside on the server to partition this processing away from your client applications.

You have a simple interface for building queries using server-side ActionScript, and an equally simple interface for running these queries from your client-side ActionScript.

Client-side ActionScript requirements

On the client side, you only need a small piece of code that establishes a connection to the Flash Remoting service and references the server-side ActionScript you want to use.

For example (notice the embedded comments):

// This #include is needed to connect to the Flash Remoting service 
#include "NetServices.as" 
 
// This line determines where Flash should look for the Flash Remoting service. 
// Ordinarily, you enter the URL to your ColdFusion server. 
// Port 8500 is the Flash Remoting service default. 
NetServices.setDefaultGatewayUrl("http://mycfserver:8500");  
 
// With the Flash Remoting service URL defined, you can create a connection. 
gatewayConnnection = NetServices.createGatewayConnection();  
 
// Reference the server-side ActionScript. 
// In this case, the stockquotes script file lives in the web root of the  
// ColdFusion server identified previously. If it lived in a subdirectory 
// of the web root called "mydir," you would reference it  
// as "mydir.stockquotes". 
stockService = gatewayConnnection.getService("stockquotes", this);  
 
// This line invokes the getQuotes() method defined in the stockquotes 
// server-side ActionScript. 
stockService.getQuotes("macr");  
 
// Once the record set is returned, you handle the results. 
// This part is up to you. 
function getQuotes_Result ( result )  
{  
    // Do something with results  
} 
Note: Client-side ActionScript does not support the two new server-side ActionScript functions, CF.query and CF.http.

Server-side requirements

Creating ActionScript that executes on the server helps leverage your knowledge of ActionScript. It also provides direct access to ColdFusion query and HTTP features. The CF.query and CF.http ActionScript functions let you perform ColdFusion HTTP and query operations.

Note: On the server side, ActionScript files use the extension .asr.

For example, the following server-side ActionScript code builds on the client-side code shown previously:

// Filename: stockquotes.asr 
// Here is the getQuotes method invoked in the client-side ActionScript. 
// It accepts a single stock quote symbol argument. 
function getQuotes(symbol)  
{  
    // Query some provider for the specified stock quote and return the  
    // results. In this case, the getQuotesFromProvider method is  
    // defined elsewhere in this ActionScript code. 
    data = getQuotesFromProvider(symbol);  
    // Return the data to the client. 
    // Note: this example does not include any of the error checking 
    // logic you would normally use prior to returning the data.  
    return data;  
} 

The getQuotes function conducts the stock quote request and returns the results of the request to the client as a RecordSet object.

Software requirements

To use server-side ActionScript files, you must have the following software installed:

  • Adobe Flash

  • ColdFusion

  • Flash Remoting Components

For more information about these products, go to www.adobe.com.

Location of server-side ActionScript files

You can place ActionScript files (*.asr) on the server anywhere below the root directory of the web server. To specify subdirectories of the web root or a virtual directory, use package dot notation (use dots instead of slashes in a fully qualified directory name). For example, in the following assignment code, the stockquotes.asr file is located in the mydir/stock/ directory:

stockService = gatewayConnnection.getService("mydir.stock.stockquotes", this);

You can also point to virtual mappings, such as cfsuite.asr.stock.stockquotes where cfsuite is a virtual mapping and asr.stock is subdirectories of that mapping.

Benefits

Server-side ActionScript lets your ActionScript engineers use their knowledge of ActionScript to write code for the back end of their SWF files, which can mean more meaningful levels of interactivity for your users. Your SWF files can share a library of server-side ActionScript functions, which means you can define functions that are tailored to your own business.

You could, for example, create a server-side ActionScript file that defines a whole library of SQL query methods. With these query methods defined on the server side, your Flash designers only have to run the specific query function they want to return data to their SWF movies. They do not have to write any SQL, and they do not have to create a query every time they retrieve data from a ColdFusion data source. It is a way of creating reusable queries that your entire Flash design team can use.

Coding the ColdFusion query and HTTP operations in ActionScript is very straightforward. The CF.query and CF.http functions provide a well-defined interface for building SQL queries and HTTP operations.

For example, the following is a typical server-side ActionScript function definition that returns query data:

// This function shows a basic CF.query operation using only  
// arguments for data source name and for SQL. 
function basicQuery() 
{ 
    mydata = CF.query({datasource:"customers",  
        sql:"SELECT * FROM myTable"});  
    return mydata; 
}

What to do next

If you are already familiar with ActionScript, here a few things to get you started:

For additional information on using Flash Remoting, see Using the Flash Remoting Service and Using Flash Remoting.