Examples
- Simple Math Service
- Advanced Math Service (uses a custom strongly-type service class)
- Step-by-step example (for developers with less ActionScript and object-oriented programming experience)
Simple Math Service
Files
(in the “Samples” folder):
- MathServiceTest.as - a class which calls remote methods and handles responses.
- MathServiceTest.fla (used to compile MathServiceTest.as, or use another ActionScript compiler)
Advanced Math Service
Files
(in the “Samples/MathServiceSubclassTest” folder)
- MathService.as - a strongly typed XML-RPC service (ServiceBase subclass) which has methods that wrap the methods exposed by the remote service.
- MathServiceSubclassTest.as - a class which contains code that creates a MathService instance and calls its methods.
- MathServiceSubclassTest.fla (used to compile MathServiceSubclassTest.as, or use another ActionScript compiler).
- Other classes used for the event framework used in MathService.as:
- IMathServiceListener.as
- MathServiceEvent.as
- event.* package described in Chapter 19 of Essential ActionScript 2.0 (Moock/O’Reilly 2004), available as a free download from http://moock.org/eas2/examples/.
Returning Struct
- Shown in the Simple Math Service example, in the "sumAndDifference" method and its related methods.
Step-by-step example
These steps walk you through the process of creating a Flash project (FLA) that makes an XML-RPC remote procedure call and displays the result on the screen. In this example, you’ll write code that will call a remote procedure that adds two numbers together. The functionality of this example is identical to the “Simple Math Service” example above—except that the example is built with the code on a keyframe on the timeline rather than in a separate class.
1. Download needed files
- Download the .zip file containing the XML-RPC client library code
- Download the other code listed in the requirements page (currently just the Flash Remoting components’ source code).
2. Set up your class path
-
If you haven’t already done so, you need to set up a “class path” in Flash—this tells Flash where to find the source code for classes that you will be using in your projects (such as the code libraries used for XML-RPC).
To set up your class path in Flash (These instructions are specific to Flash 8, but should be similar in Flash MX 2004):
- Create a folder on your hard drive where you want to store third-party code libraries that you’ll use in various Flash projects. For example, I created a folder in My Documents > Flash Projects > class path.
- In Flash, add your “class path” folder to the ActionScript class path:
- Choose Edit > Preferences (Win) or Flash > Flash Preferences (Mac)
- Choose the “ActionScript” category
- Click the “ActionScript 2.0 Settings” button
- Click the “+” button
- Click the “target” button (the circle with a crosshair)
- In the dialog box, select your “class path” folder created in Step 1. Click OK.
- Click OK to close the Preferences dialog.
3. Add the libraries to your class path
-
Next you need to copy the code libraries to your class path folder. Unzip the downloaded files and paste the entire source code structure into a folder that's in your class path (such as the one created in (2) above).
Note that it’s important to maintain the folder/file structure of the source code. Usually source code has a top-level folder. For instance, for the XML-RPC library the top-level folder is the “com” folder—all the other source code is contained in that folder. The “com” folder must be directly inside the folder that you specified as your class path folder, or else Flash won’t be able to find your source code. Likewise, for the Flash Remoting components the top-level folder is the “mx” folder, so you must copy the “mx” folder (and its contents) and place the “mx” folder directly into your class path folder.
4. Write the ActionScript
- Create a new Flash document (FLA) and save it to your computer.
- Select the keyframe at Frame 1 of the timeline, and open the Actions panel
-
First, you need to add code to “import” the classes you’ll be using—basically you are telling Flash the fully-qualified name of the classes, so Flash knows where in the class path to find them:
import com.probertson.xmlrpc.Service; import com.probertson.xmlrpc.PendingCall; import com.probertson.rpc.RelayResponder2; import mx.rpc.ResultEvent; import mx.rpc.FaultEvent;
-
Next you need to write a couple of functions (the “event handlers”). One will be called if the remote procedure call works, and the other will be called if there’s an error (“fault”) in the process of making the remote call. Since this example will be calling a remote procedure that adds two numbers, these functions will be called
sumResult()andsumFault().function sumResult(event:ResultEvent):Void { // do something with the result trace("result = " + event.result.toString()); } function sumFault(event:FaultEvent):Void { // do something with the error trace("there was an error!" + event.fault.faultstring); } Finally you need to write the code that makes the RPC call and “subscribes” your event handler functions so that they get the appropriate notifications.
// create the service var service:Service = new Service("http://www.cookcomputing.com/xmlrpcsamples/math.rem", "math", null, null); // make the xmlrpc call var myPendingCall:PendingCall = service.sum(2, 3); // wire up the event handlers -- notice that the last two parameters // match the names of your event functions myPendingCall.responder = new RelayResponder2(this, sumResult, sumFault);In the
Service()constructor call, the first parameter is the url of the XML-RPC service. The second parameter is the “service name”—see “A note on terminology” in the Design page for more details on that parameter. The actual remote method name is used as the name of the method that's called on the Service instance (service.sum()), and the parameters to send to the remote service are passed as parameters to that method call.- Run the code. It sets up the remote service, makes the XML-RPC call, and when a result comes back it calls your
sumResult()function, passing an event object that includes the result value as one of its properties. Alternatively, if there’s an error with the XML-RPC call, it calls yoursumFault()function with information about the error.