Fun with code (since 2006)

Making Flash Remoting easier in Flash CS3


01.24.08 Posted in Actionscript 3 by

Here is something I have just added into my book in the Flash Remoting chapter and wanted to share with you :)

As you may know, no wrapper class is embedded into Flash CS3 for your remoting calls, you have to use the raw NetConnection class which is not very practical in a large project.

I wanted a simple class to call some remote methods in Flash CS3 like the RemoteObject class does in Flex, and like the Service class we had in Flash 8. I came across Danny Patterson remoting framework which is very smooth, but noticed that it was not working the way I wanted it.

So I created a wrapper around the NetConnection object which behaves like the Service class we used to have with a PendingCall assigned to each remote procedure.

Here is how you create the remote service :

// create a remote object (Singleton)
var service:Service = Service.getInstance();

// set the required infos
service.setRemoteInfos ("http://www.bytearray.org/wp-content/projects/remotingservice/amfphp/gateway.php",
"org.bytearray.HelloWorld",
ObjectEncoding.AMF3 );

Then you call the remote method, this returns you a PendingCall object :

// call the remote method
var rpc:PendingCall = service.sayHello("Hello there ?");

The PendingCall object is dispatching two events, ResultEvent.RESULT and FaultEvent.FAULT :

// call the remote method
var rpc:PendingCall = service.sayHello("Hello there ?");

// wait for the result event (everything is ok)
rpc.addEventListener ( ResultEvent.RESULT, success );

// wait for the fault event (an error occured)
rpc.addEventListener ( FaultEvent.FAULT, error );

// get the result
function success ( pEvt:ResultEvent ):void
{

// output : you said : Hello there ?
trace( pEvt.result )

}

// get the result
function error ( pEvt:FaultEvent ):void
{

trace( pEvt.fault.description )

}

You can change the service or gateway at any time :

service.service = "org.bytearray.AnotherService";
service.gatewayURL = "http://www.myserver.org/amfphp/myOtherGateway.php";

Or authenticate yourself with some credentials :

service.setRemoteCredentials ("bob", "groove");

Sources and file examples available here

Update (05/04/09) : Version 0.2 : Changed the Service implementation as a Singleton.



35 Responses to “Making Flash Remoting easier in Flash CS3”

  1. frog says:

    Nice one Jimmy !

  2. Thibault Imbert says:

    haha you’re welcome froggy ;)

  3. Marc says:

    funny i thought the same about the as3 lightweight remoting framework (missing listeners on calls) and just as i started to write my own implementation i found the SSR (super simple remoting ;)). it works as expected and saved some time. but thanks for your implementation none the less *gg*

  4. Mike says:

    Can timeout interval be adjusted?

  5. William from Lagos says:

    What’s the title of your book by the way?

  6. Thibault Imbert says:

    Hi Mike,

    yes such a feature can be added, I will add that :)

    Hi William,

    The book is called “Pratique d’ActionScript 3″ and is written in French. O’Reilly should translate it after the release !

    link to the book :

    http://www.oreilly.fr/catalogue/2841774457

    regards,

    Thibault

  7. Thibault Imbert says:

    Hi Marc,

    very nice !

    Maybe we could merge our two projects ?

    What do you think ? :)

    best,

    Thibault

  8. Marc says:

    Hi Thibault, SSR was written by Aaron Smith (http://blog.rubyamf.org/) and not by me. I just use it ;)

  9. Thibault Imbert says:

    hehe ok Marc !

    thanks :)

  10. Moïse says:

    Very nice,
    I had started started to write my own remoting class
    I’m going to try it :-)

  11. paul says:

    Why not just import the rpc.swc from the Flex SDK, why reinvent the wheel here… Interesting solution however.

  12. Thibault Imbert says:

    Hi paul,

    I saw quite complicated hacks to do that, did you try it ?

    If it works smoothly, that would be nice :)

    let me know

    cheers

  13. Thomas says:

    Hi Thibault,

    thanks for sharing your solution with us. May I ask, under which licence you’re publishing the code? Am I allowed to use it in commercial projects and am I allowed to modify it to my special needs?

  14. Thibault Imbert says:

    Hi Thomas,

    There is no licence. You can modify the code and do whatever you want with it :)

    cheers,

    Thibault

  15. CG says:

    Question…how can I make Flash remoting -> ColdFusion secure?

    It seems that you have to set a CFC to access=”remote” but that allows anyone in the world to run it as well.

  16. gregpich says:

    Hi Thibault,
    I’m looking for a french tutorial video that you have done, about AS3 and amfphp, but I can’t find it anymore on the web. May you please post the link?

  17. TrAvIkK says:

    Hello :)

    First of all, I wanted to say, that you rock man :) Waiting for your book to see it in English :)

    Here is what i wanted to admit: In some way, FaultEvent is not dispatched, or catched by event listener, in Flex 3.

  18. SiggeLund says:

    I’m having problems getting this to work with query returns from ColdFusion.

    var results:Object = pEvt.result;
    var queryItems = results.items;
    for (var queryIndex=0; queryIndex<queryItems.length; queryIndex++)
    {
    }

    This snippet won’t work since the items object is undefined. Anyone know how this should be done in AS3?

  19. SiggeLund says:

    Figured it out. This code snippet can be used to find the old style Recordset

    /*
    Returns an array with objects, that works the same way as the results.items did in AS2.

    Return events from AS3 Flash Remoting calls only contain one member when returning,
    and it’s called “serverInfo”. Pass that object to this method, and the normal array is then returned.
    Stupid that Adobe has not implemented this themselves…

    Example:
    function listEvents_Result( pEvt: ResultEvent ):void
    {
    var results = pEvt.result;
    results["items"] = RemotingUtil.getRecordSet(results.serverInfo);

    for (var queryIndex=0; queryIndex<results.items.length; queryIndex++)
    {
    var queryObj = results.items[queryIndex];
    trace(queryObj.name);
    trace(queryObj.date);
    }
    }

    In this example we assume that the recordset has the two columns “name” and “date”.

    */
    public static function getRecordSet(serverInfo:Object):Array
    {
    var queryItems = serverInfo.initialData;
    var queryColumns = serverInfo.columnNames;
    var retArray:Array = new Array();

    for (var queryIndex=0; queryIndex<queryItems.length; queryIndex++)
    {
    var queryObj = new Object();
    for (var j=0; j<queryColumns.length; j++)
    {
    queryObj[queryColumns[j]] = queryItems[queryIndex][j];
    }
    retArray.push(queryObj);
    }
    return retArray;
    }

  20. Thibault Imbert says:

    Hi SiggeLund,

    Yes, in Flash CS3 when you are receiving a query from the server you receive an objet with a serverInfo property that you have to parse the construct an appropriate RecordSet.

    Adobe decided to remove the Flash Remoting components from Flash CS3 which is in my opinion a real mistake. Flash CS4 should solve that limitation ;)

  21. SiggeLund says:

    Thibault:

    Tell me about it… Good thing that I found the tool ServiceCapture, would have been a bit confused otherwise. :)

  22. Thibault Imbert says:

    Hi SiggeLund,

    I cannot talk about it for the moment ;), but as soon as possible I will post a tutorial here about the new capabilities offered by Flash CS4 in terms of Remoting.

    Yes ServiceCapture is a must-have when it comes to Remoting debugging. ;)

    best,

    Thibault

  23. SiggeLund says:

    Sounds great, I will look in from time to time to see what you’re cooking. =)

  24. Hi Thibault, we also implemented a remoting package in our as3 opensource project (we are a commercial company) recently launched at http://www.dpdk.nl/opensource.
    It also features built in support for recordsets which is a datatype instead of a multidimensional array as presented in the example above.

    It also tackles the problem you tackled with danny patterson’s implementation, which is that you can only call one method on one service. Both your and our solution fixed that by providing handlers/events for each method called on the service

  25. [...] We also found another remoting implementation in as3 which is also cool, but lacking resultsets, on bytearray.org « AsUnit modification for asynchronous testing The Log and LogEvent classes. [...]

     
  26. John Barrett says:

    Thanks for the post`-`
    I have been going crazy trying to use flash remoting in CS3 with a cfc backend, and with no luck.

    I am at school now, but I am going to try this when I get home.

    Johnny

  27. [...] Flex中有RemoteObject类来实现Remoting,这个类无法在Flash IDE中使用。要在Flash CS3中实现Remoting,我们可以借助bytearray.org上面提供的一些类来完成。你可以在这个地址中下载相关的类文件和范例: http://www.bytearray.org/?p=122 [...]

     
  28. Kid Hat says:

    thank you for sharing

  29. Nathan says:

    Thank you VERY MUCH. I have been trying to get AMFPHP to work correctly using several tutorials, all with no luck, and I finally got this one to work!

  30. iguess says:

    How can I set remote info for local coldfusion server? An example would be great. Thanks

  31. Matt says:

    Ive got this perfectly implemented using a live gateway. However, for our development pages, we have the entire development site password protected. When I attempt to access the development gateway page, I get an error saying Flash cannot open the page. I know this has to do with the page authentication. Is there any sort of bypass on Flash’s end that will take care of this. With the Flash 8 MX package we dont have this issue, but we obviously cant use that package with an AS 3.0 swf.

Leave a Reply

Open Sort Options

Sort comments by:
  • * Applied after refresh

ByteArray.org
Fun with code since 2006.