Using Flash Remoting with server-side ActionScript
111
Using Flash Remoting with server-side ActionScript
The ability to create server-side ActionScript provides a familiar way for Flash developers to access
ColdFusion query and HTTP features without learning CFML. Identify the ActionScript files
(with the .asr extension) that you want to call from the Flash application and place them on the
server, anywhere under the web server’s root directory. To specify subdirectories of the web root or
a virtual directory, use package dot notation. For example, in the following assignment code, the
stockquotes.asr file is in the mydir\stock\ directory:
var stockService:Service =
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 a subdirectory of that mapping. The
CF.query
and
CF.http
functions give you a well-defined interface for building the SQL queries and HTTP
operations of ColdFusion.
For example, the following server-side ActionScript function definition returns a RecordSet
object:
function basicQuery()
{
var mydata = CF.query({datasource:"customers",
sql:"SELECT * FROM myTable"});
return mydata;
}
Using CF.http()
The
CF.http()
ActionScript function lets you retrieve information from a remote HTTP server.
HTTP
Get
and
Post
methods are supported. Using the
Get
method, you send information to
the remote server directly in the URL. This method is often used for a one-way transaction in
which the
CF.http()
function retrieves an object, such as the contents of a web page. The
Post
method can pass variables to a form or CGI program, and can also create HTTP cookies.
One of the most basic ways to use the
CF.http()
function is with the
Get
method argument to
retrieve a page from a specified URL. For example, the following server-side code retrieves file
content from the specified URL:
function basicGet(url)
{
// Invoke with just the url. This is an http get.
result = CF.http(url);
return result.get("Filecontent");
}
In the client-side ActionScript, you call the service function and display the results in the Flash
application, as in the following example:
import mx.remoting.Service;
import mx.remoting.PendingCall;
import mx.rpc.RelayResponder;
import mx.rpc.ResultEvent;
var myHttpService:Service = new Service(