Flash Remoting is a technology for making remote procedure calls from Flash Player to server-side services. The concept is similar to that of Web services: The client makes a request to a method that's exposed using a server-side service. The request is serialized in a specific format, sent over HTTP (or HTTPS), and the response is likewise serialized and sent back to the client. This approach enables remote clients and services to interact to create integrated applications. Like Web services, Flash Remoting has the advantage of automatically serializing and deserializing to and from native data types. However, there are significant differences between Flash Remoting and Web services. Following are a few of those differences:
Understanding Flash Remoting BasicsWhen you want to use Flash Remoting, there are two elements that communicate: a client and a service. The service is generally a class that has been exposed so that it is available to Flash Remoting. Flash Remoting is a request-response model, which means the client must initiate all requests. The client must make all calls through an intermediary called a gateway. The gateway is a web-accessible resource on the server that is capable of accepting AMF requests, deserializing them, and delegating the requests to the appropriate services. There are many Flash Remoting gateway products, including the following:
Each gateway has its specific installation instructions. However, after you've installed the gateway, the general instructions to use it are the same. The following section looks at the ActionScript required to make Flash Remoting calls. In each case, you'll need to know the URL to the gateway resource for your server. The documentation for the specific gateway you are using will tell you what you need to know to locate the correct resource. It is always a web-accessible resource such as a page (a PHP page, a .NET page, and so on) or a servlet. Making Flash Remoting CallsAlthough the Flex framework and the Flash Remoting components for Flash provide a high-level API for working with Flash Remoting, at a lower level, all Flash Remoting calls go through a flash.net.NetConnection object. In this section, we'll look exclusively at working directly with NetConnection objects to make Flash Remoting calls. The first step in making Flash Remoting calls is to construct a NetConnection object using the constructor. The constructor does not require any parameters, as you can see here: var netConnection:NetConnection = new NetConnection(); Next, you must specify the gateway URL using the connect() method. Despite its name, the connect() method does not actually attempt to make a connection to the resource. It simply sets the gateway URL for the object so that subsequent Flash Remoting calls can be routed through the correct gateway resource. netConnection.connect("http://localhost/gateway"); After you've set the gateway URL, you can make calls to methods of available services using the call() method. The call() method requires at least two parameters: the name of the service and method as a string, and an object indicating how to handle responses. The name of the service and method must be the fully qualified service name and method name, all in dot notation. That means that if the service is in a package, you must specify the package as well as the name of the service. The name of the service and the name of the method are also separated by a dot. The following statement makes a call to the test method of the Example service: netConnection.call("Example.test", null); The second parameter can be null (as in the preceding example) if and when you do not need to listen for a response from the service method. However, if you do need to listen for a response, you can use a flash.net.Responder object. The Responder constructor requires that you specify two functions to handle the possible responses: a result and an error. The following statement makes a call to the test method, this time handling the response: netConnection.call("Example.call", new Responder(onResult, onError)); When the service method returns a valid value, the result method gets called and is passed the return value as a parameter. When an error occurs, the error method gets called with an Object parameter whose properties detail the error. You can also pass parameters to the service method by adding additional parameters to the call() method parameter list. For example, the following code passes the parameters 1, TRue, and "a" to the service method: netConnection.call("Example.call", new Responder(onResult, onError), 1, true, "a"); The following rewrite of the LimerickData class uses Flash Remoting instead of loading data using URLLoader. For the sake of simplicity, this example does not handle errors. In an actual application, you might want to handle errors by bubbling up error events (as described in Chapter 13, "Working with Events"). package com.peachpit.aas3wdp.limerickreader.data { import flash.events.EventDispatcher; import flash.events.Event; import flash.net.NetConnection; import flash.net.Responder; public class LimerickData extends EventDispatcher { private var _limerick:String; private var _ids:Array; private var _pendingNext:Boolean; private var _netConnection:NetConnection; public function get limerick():String { return _limerick; } public function LimerickData() { _limerick = ""; // Use a NetConnection object rather than the // URLLoader object used previously. _netConnection = new NetConnection(); _netConnection.connect("http://www.rightactionscript.com/limerick/gateway.php"); _netConnection.call("LimerickService.getIds", new Responder(setIds, null)); } private function setIds(ids:Array _ids = ids; if(_pendingNext) { next(); } } public function next():void { if(_ids != null) { var index:uint = _ids[Math.floor(Math.random() * _ids.length)]; _netConnection.call("LimerickService.getLimerick", new Responder(onData, null), index, false); } else { _pendingNext = true; } } private function onData(limerick:String _limerick = limerick; dispatchEvent(new Event(Event.CHANGE)); } } } This version accomplishes the same basic tasks as previous versions. However, it now uses Flash Remoting to accomplish these goals. |