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.
Comments (28)
Nice one Jimmy !
haha you’re welcome froggy
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*
whoops, the link: http://osflash.org/projects/ssr
Can timeout interval be adjusted?
What’s the title of your book by the way?
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
Hi Marc,
very nice !
Maybe we could merge our two projects ?
What do you think ?
best,
Thibault
Hi Thibault, SSR was written by Aaron Smith (http://blog.rubyamf.org/) and not by me. I just use it
hehe ok Marc !
thanks
Very nice,
I had started started to write my own remoting class
I’m going to try it
Why not just import the rpc.swc from the Flex SDK, why reinvent the wheel here… Interesting solution however.
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
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?
Hi Thomas,
There is no licence. You can modify the code and do whatever you want with it
cheers,
Thibault
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.
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?
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.
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?
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;
}
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
Thibault:
Tell me about it… Good thing that I found the tool ServiceCapture, would have been a bit confused otherwise.
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
Sounds great, I will look in from time to time to see what you’re cooking. =)
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
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
thank you for sharing
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!
Trackbacks/Pingbacks (5)
[...] check it [...]
[...] http://www.bytearray.org/?p=122 [...]
[...] 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. [...]
[...] Flex中有RemoteObject类来实现Remoting,这个类无法在Flash IDE中使用。要在Flash CS3中实现Remoting,我们可以借助bytearray.org上面提供的一些类来完成。你可以在这个地址中下载相关的类文件和范例: http://www.bytearray.org/?p=122 [...]
[...] ByteArray.org By: Thibault Imbert [...]