- Native; data is ready to use the moment it loads as native ActionScript objects.
- No processor-intensive parsing, etc.
- No plumbing code for deserializing requests.
- Public gateway and APIs for mobile mashups.
- Only RPC solution for Flash Lite.
- Easy to understand and use.
Monthly Archive for August, 2007
Check out the MiniFlickr sample application that comes with SWX PHP for an example of how to build data-driven Flash Lite applications using SWX.
Keep in mind that, due to a bug in Flash Lite, you cannot make POST requests in a loadMovie call. Instead, you must make GET requests when using SWX RPC with Flash Lite. For more information on this, see the Mobile Limitations section, below.
The MiniFlickr application makes use of the ExternalAsset and LoadManager classes that come with SWX AS to queue all load requests (data and assets such as images). This is essential in mobile development as most handsets will fail if you try to load two things at once. The SWX AS Full API handles the queueing of requests for you and gives you a general purpose load queue that you can use in your own applications. Make sure you study the MiniFlickr sample if you want to create Flash Lite applications using SWX PHP.

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.
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.
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.
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.
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.
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.
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.
Download the Adobe Illustrator file for Datum. You can also print your own SWX Moo cards and SWX Stickers that feature Datum.
