Examples

Simple Math Service

Files

(in the “Samples” folder):

Advanced Math Service

Files

(in the “Samples/MathServiceSubclassTest” folder)

Returning Struct

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

  1. Download the .zip file containing the XML-RPC client library code
  2. Download the other code listed in the requirements page (currently just the Flash Remoting components’ source code).

2. Set up your class path

  1. 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):

    1. 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.
    2. In Flash, add your “class path” folder to the ActionScript class path:
      1. Choose Edit > Preferences (Win) or Flash > Flash Preferences (Mac)
      2. Choose the “ActionScript” category
      3. Click the “ActionScript 2.0 Settings” button
      4. Click the “+” button
      5. Click the “target” button (the circle with a crosshair)
      6. In the dialog box, select your “class path” folder created in Step 1. Click OK.
      7. Click OK to close the Preferences dialog.

3. Add the libraries to your class path

  1. 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

  1. Create a new Flash document (FLA) and save it to your computer.
  2. Select the keyframe at Frame 1 of the timeline, and open the Actions panel
  3. 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;
  4. 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() and sumFault().

    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);
    }
  5. 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.

  6. 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 your sumFault() function with information about the error.