Recipe 19.6. Calling Flash Remoting Methods with Components


Problem

You want to call a Flash Remoting method without having to write much Action-Script.

Solution

Use the RemotingConnector component.

Discussion

Flash Remoting is a technology that enables incredibly effective and efficient client server communications with Flash applications. Flash Remoting works in a manner similar to that of web services in many respects. It allows Flash clients to call methods of services that may be written in Java, PHP, .NET, or other languages. However, Flash Remoting uses a binary messaging format called AMF that is native to Flash Player. Thus Flash Remoting is often significantly faster than web services.

Flash Remoting requires a server-side gateway that is capable of receiving the AMF requests, delegating the requests to the appropriate services, and returning the AMF responses. ColdFusion ships with a Flash Remoting gateway by default. So does JRun. Macromedia also sells Flash Remoting gateway products for J2EE application servers and .NET. You can learn more about the Macromedia Flash Remoting products at http://www.macromedia.com/software/flashremoting. In addition, there are non-Macromedia Flash Remoting gateway products. WebOrb (http://www.themidnightcoders.com) is available in both standard (free) and professional editions. WebOrb is available in both J2EE and .NET versions. FLAP (http://www.simonf.com/flap) is an open source product for Perl and Python. AMFPHP (http://www.amfphp.org) is an open source product for PHP. And OpenAMF (http://www.openamf.org) is an open source product for J2EE.

Although to utilize Flash Remoting fully it's recommended that you use Action-Script, the necessary ActionScript may involve a relatively steep learning curve for a Flash developer with little ActionScript experience. For basic Flash Remoting operations, you can use the RemotingConnector component that requires little to no Action-Script. The RemotingConnector component is the focus of this recipe.

The RemotingConnector component does not ship with Flash. However, if you have Flash Professional, you can download the RemotingConnector component for free from http://www.macromedia.com/software/flashremoting/downloads/components. When you've installed the component, it will be available from the Data Components folder of the Components panel.

The first step is to add an instance of RemotingConnector to the stage. You can drag an instance from the Components panel to the stage, and then assign the component an instance name. After you've assigned an instance name, you can assign values to parameters. To do so, select the instance and open the Component Inspector panel. Then assign values to the following parameters:


gatewayUrl

The URL to the gateway. The gateway is the server resource that parses AMF and delegates the requests to the appropriate services. It's a servlet, ASP.NET page, PHP page, and so on, and it's always the same regardless of what service you are requesting.


serviceName

The name of the service you are requesting. Generally, it's the fully qualified class name.


methodName

The name of the method from the service that you want to call.

Unlike the WebServiceConnector component, the RemotingConnector component doesn't automatically populate the schema. That means that you have to correctly define the schema. With the RemotingConnector component, you generally have to define the schema by way of the add and delete field buttons in the Schema tab. From the Component Inspector panel, select the Schema tab. Then use the add and delete buttons in the upper-left corner of the tab. The button on the far left adds a new top-level field. Generally that is unnecessary, as Flash Remoting operations consist of requests (params) and responses (results). You can use the second button to define new fields nested under params and/or results. For example, if a Flash Remoting method requires two parameters, you would want to define two fields nested under params. For each field you can use the name/value portion of the panel to define the correct name and data type.

As with the XMLConnector and WebServiceConnector components, you need to tell Flash when to make the Flash Remoting request. You can accomplish that with the trigger( ) method. The RemotingConnector class defines a trigger( ) method that you can call from the instance. Place the ActionScript code on the first keyframe if you want to make the call as soon as the application starts. Place the code within a button event handler method if you want to make the call when the user clicks a button.

You can download a working example file from http://www.rightactionscript.com/fcb.




Flash 8 Cookbook
Flash 8 Cookbook (Cookbooks (OReilly))
ISBN: 0596102402
EAN: 2147483647
Year: 2007
Pages: 336
Authors: Joey Lott

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