Recipe 21.4. Calling Flash Remoting Methods


Problem

You want to call a Flash Remoting method.

Solution

Use a NetConnection object, connect to the Flash Remoting gateway and use the call( ) method to call the method.

Discussion

Flex and Flash have ActionScript-based APIs that are designed to simplify Flash Remoting method calls. However, this recipe discusses the low-level solution that relies only on the intrinsic (native to Flash Player) solution.

All Flash Remoting operations rely on the flash.net.NetConnection class. The first step when making Flash Remoting method calls is to construct a new NetConnection object. You should use one NetConnection object per Flash Remoting service (a service generally being a class):

var connection:NetConnection = new NetConnection(  );

Next, call the connect( ) method, passing it the URL to the Flash Remoting gateway. For example, if you are using ColdFusion running locally as a standalone product, then try the following:

connection.connect("http://localhost:8500/flashservices/gateway/");

The following connects to a working example AMFPHP Flash Remoting gateway:

connection.connect("http://www.rightactionscript.com/flashremoting/gateway.php");

If you don't know which gateway URL to use, consult either the documentation for the Flash Remoting server product or your network administrator responsible for managing the server portion of Flash Remoting.


The connect( ) method does not immediately attempt to connect to the gateway URL. If the URL is invalid, or if there is a server-side error, you won't receive an error until you actually call a method.

The next step is to call a Flash Remoting method using the call( ) method of the NetConnection object. The call( ) method requires two parameters. The first parameter specifies the name and path of the service and method. The second parameter tells the NetConnection object how to handle the response from the method call. For a discussion of how to handle responses from Flash Remoting, see Recipe 21.2. In this initial discussion, it's assumed that the application doesn't need to handle a response; as such, a null value is passed for the second parameter.

The first parameter for the call( ) method specifies the method using a dot-delimited path in the following format:

package.ServiceObject.method

For example, if you are using a ColdFusion component (CFC) accessible at http://localhost:8500/com/oreilly/as3cb/Example.cfc, and the CFC defines a method called test( ), the following code calls the method:

connection.call("com.oreilly.as3cb.Example.test", null);

You can pass parameters to the method by adding them to the parameter list of the call( ) method. For example, if the test( ) method accepts a number and a string parameter, you can use the following code to pass those values:

connection.call("com.oreilly.as3cb.Example.test", null, 15, "abcd");

See Also

Recipes 21.2 and 21.5




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net