Archive for the 'Documentation' Category

SWX instance: how to set a fault handler

The SWX class also provides a fault handler that you can use to handler errors during SWX RPC calls. To test it out, modify your code so that it matches the listing below:

import org.swxformat.SWX;
 
var swx:SWX = new SWX();
swx.gateway = "http://localhost:8888/php/swx.php";
swx.encoding = "GET";
swx.debug = true;
swx.timeout = 2; // seconds
 
var callParameters:Object =
{
    serviceClass: "Calculator",
    method: "addNumbers",
    args: [35, 7],
    result: [this, resultHandler],
    timeout: [this, timeoutHandler],
    fault: [this, faultHandler]
}
 
swx.call(callParameters);
 
function resultHandler(event:Object)
{
    status.text = event.result;
}
 
function timeoutHandler()
{
    status.text = "Call timed out!";
}
 
function faultHandler(event:Object)
{
    status.text = event.fault.message;
}

And modify your class so that it generates an error:

<?php
    class Calculator
    {
        function addNumbers($n1, $n2)
        {
            return $n3; // $n3 does not exist!
        }
    }
?>

Test your FLA and you should get something along the lines of Error 8: Undefined variable: n3 in /htdocs/swx/trunk/php/services/Calculator.php, line 7 in the status text field in Flash.

The fault handler also returns API-specific fault codes (e.g., Flickr API error codes) back to Flash.

SWX instance: how to set a timeout handler

You can also specify a timeout handler for your SWX instance to handle calls that take too long to return a result.

Modify the listing so that it matches the one below:

import org.swxformat.SWX;
 
var swx:SWX = new SWX();
swx.gateway = "http://localhost:8888/php/swx.php";
swx.encoding = "GET";
swx.debug = true;
swx.timeout = 2; // seconds
 
var callParameters:Object =
{
    serviceClass: "Calculator",
    method: "addNumbers",
    args: [35, 7],
    result: [this, resultHandler],
    timeout: [this, timeoutHandler]
}
 
swx.call(callParameters);
 
function resultHandler(event:Object)
{
    status.text = event.result;
}
 
function timeoutHandler()
{
    status.text = "Call timed out!";
}

The default timeout duration is 30 seconds but you can override that, as shown here.

In order to make the call actually time out, modify the Calculator class in PHP too to make it sleep for 10 seconds before returning the result. The Calculator class should match the one in the listing below:

<?php
class Calculator
{
    function addNumbers($n1, $n2)
    {
        sleep(10); // Make the call time out!
        return $n1 + $n2;
    }
}
?>

Now test your Flash movie and, after two seconds, you should see the SWX call time out. Timed-out calls are cancelled and will not trigger the result handler at any point in the future.

Instantiating the SWX class

Ideally, you should not have to manually create movie clips and work with low-level native constructs like loadMovie() when working with SWX RPC on a daily basis. The SWX ActionScript Library gives you an abstract, high-level interface for working with SWX RPC.

To use this high-level interface, you need to instantiate the SWX class.

Modify the code listing so that it matches the one below:

import org.swxformat.SWX;
 
var swx:SWX = new SWX();
swx.gateway = "http://localhost:8888/php/swx.php";
swx.encoding = "GET";
swx.debug = true;
 
var callParameters:Object =
{
    serviceClass: "Calculator",
    method: "addNumbers",
    args: [35, 7],
    result: [this, resultHandler]
}
 
swx.call(callParameters);
 
function resultHandler(event:Object)
{
    status.text = event.result;
}

When you run the above example, you should again see 42 in the status text field. Let's look at what has changed.

The biggest change is that you are actually instantiating the SWX class and setting certain properties there (such as the gateway URL, encoding method and whether you want debug information for calls).

Instead of putting call-related parameters directly into a movie clip (you can delete the dataHolder movie clip now as you aren't using it any more), you create a callParameters object and specify the serviceClass, method, and args properties there. But you can do more, you can also specify a result handler that will get called once the data has loaded.

The result handler receives an event object as an argument. That event object has a result property that points to the loaded data.

SWX ActionScript Library: using the prepare() method of the SWX class

When working with SWX RPC, you don't want to continuously serialize complex arguments by hand when call server-side methods. And you don't have to, if you use the prepare() method of the SWX class in the SWX ActionScript Library that comes with SWX PHP.

Save your FLA in the flash/ folder (the one that has the org and com folders) before continuing. (Or add that folder to your ActionScript classpath in the Flash IDE and save your FLA anywhere.)

Modify the code sample so that it matches the listing below:

import org.swxformat.SWX;
 
dataHolder.serviceClass = "Calculator";
dataHolder.method = "addNumbers";
dataHolder.args = [35, 7];
dataHolder.debug = true;
 
SWX.prepare(dataHolder);
 
dataHolder.loadMovie("http://localhost:8888/php/swx.php", "GET");
 
function onEnterFrame()
{
    status.text = dataHolder.result;
}

The prepare() static method of the SWX class simply serializes your arguments into JSON format for you. Notice that the args property in your dataHolder is no longer a string but an ActionScript array with two numbers inside it.

This is definitely far better than manually serializing your arguments by hand but it's still not ideal, is it? For one thing, that onEnterFrame function we're using is not a good practice. It would be really nice if we could have an event handler called when the data arrives instead of polling for it. The SWX ActionScript Library provides that functionality for you too.

Debugging with the SWX Data Analyzer

You can see the data that is returned from an SWX call using the SWX Data Analyzer if you turn debug mode on.

To turn debug mode on, set a property called debug in your data holder movie clip. For example:

dataHolder.debug = true;

If you're using the SWX ActionScript Library, set the debug property on your SWX class instance to true.

var swx:SWX = new SWX();
swx.debug = true;

Once you’ve set debug mode on, start the SWX Data Analyzer and test your Flash project. You will see the returned data appear in the SWX Analyzer.

Make sure you start the SWX Data Analyzer before you run any SWF files that use it. Also make sure that there is only one instance of the SWX Data Analyzer running at any one time.

SWX Analyzer is a Flex application. An Adobe AIR (desktop) version is now also available for OS X and Windows.

SWX Service Explorer

You can use the SWX Service Explorer to explore server-side services on your development machine.

Use the SWX Service Explorer on the SWX Public Gateway to explore the publicly-available SWX APIs on swxformat.org. You can call these APIs directly from Flash without writing any server-side code or hosting your own SWX RPC gateway.

The SWX Philosophy

SWX is designed to provide Flash developers with a simple and enjoyable experience when building data-driven applications in Flash. The key words here are simple, enjoyable and experience.

Simple: SWX is as simple as possible but no simpler. If something can be made simpler without sacrificing essential core functionality, I’ll make it simpler.

Enjoyable: Because life should be fun. The SWX tools aim to put a smile on your face, do the heavy lifting, provide a seamless experience and be aesthetically pleasing so you’ll be inspired todo the same in your applications.

Experience: These core tenets apply to the whole experience of SWX, not just the API or codebase. That includes the web site, documentation and tools. The focus is to make it as easy as possible for you to get up and running with SWX from the moment you first visit the SWX web site. I call this Systemwide Simplicity.

Who’s the cute little guy, then?

If you’re asking about the SWX mascot/logo, his name’s Datum. He lives in a SWF shell.

Download the Adobe Illustrator file for Datum. You can also print your own SWX Moo cards and SWX Stickers that feature Datum.

How do you pronounce SWX?

It's pronounced "swix".

The Public SWX Gateway

There is a public gateway that you can use on swxformat.org to call the various SWX APIs without hosting your own PHP installation of SWX.

Take a look at the Start Page for the SWX public gateway. This is the same Start Page that you will see when you install SWX PHP or the SWX PHP MAMP Bundle (only it's called start.php instead of index.php here on swxformat.org).

The URL for the public gateway is:

http://swxformat.org/php/swx.php/

You are encouraged to make use of the SWX public gateway but please do not flood it with so many requests that it resembles a denial of service attack!

The public gateway also hosts versions of the SWX Service Explorer and SWX Data Analyzer.




Bad Behavior has blocked 2978 access attempts in the last 7 days.