AS3 FLASH REMOTING EXAMPLE

MONDAY, MARCH 12 2007 @ 10:24 AM

Using remoting in AS3 is actually quite simple. Remoting calls are now made directly through NetConnection, so there's no need for Remoting classes or components, everything you need is readily available. Here is a quick example.

This is a wrapper class for NetConnection that makes sure we are using the right AMF encoding. The default is AMF3, which is not supported on AMFPHP versions lower than 2.0. So, we use AMF0 instead:

  1. package
  2. {
  3. import flash.net.NetConnection;
  4. import flash.net.ObjectEncoding;
  5. public class RemotingService extends NetConnection
  6. {
  7. function RemotingService(url:String)
  8. {
  9. // Set AMF version for AMFPHP
  10. objectEncoding = ObjectEncoding.AMF0;
  11. // Connect to gateway
  12. connect(url);
  13. }
  14. }
  15. }

The next class is just a test that uses the previous class to make a call.


  1. package
  2. {
  3. import flash.net.Responder;
  4. public class RemotingTest
  5. {
  6. private var rs:RemotingService;
  7. function RemotingTest()
  8. {
  9. init();
  10. }
  11. private function init()
  12. {
  13. rs = new RemotingService("http://your.domain.com/amfphp/gateway.php");
  14. var responder:Responder = new Responder(onResult, onFault);
  15. var params:Object = new Object();
  16. params.arg1 = "something";
  17. params.arg2 = "2";
  18. rs.call("Class.method", responder, params);
  19. }
  20. private function onResult(result:Object):void
  21. {
  22. trace(result);
  23. }
  24. private function onFault(fault:Object):void
  25. {
  26. trace(fault);
  27. }
  28. }
  29. }

Given the following PHP class, the previous exercise could be easily adjusted to print "something and 2" to the Output panel. Just copy it to the 'services' folder of your AMFPHP installation, and replace "Class.method" with "Test.test" on the RemotingTest class.

Test.php


class Test
{

function __construct()
{
$this->methodTable = array(

"test" => array(
"description" => "Tests service",
"access" => "remote"
)
);
}


function test($params)
{
return $params['arg1'] . " and " . $params['arg2'];
}

}

?>


Archived under: PHP, Flash, ActionScript 3.0. | Permalink | google | del.icio.us Is it delicious? | digg Do you digg it?


OSCAR TRELLES

MARCH 12 2007 @ 10:42 AM

I know, I know... I need some code highlighting thingy here.

DRU

MARCH 14 2007 @ 04:15 PM

Cool!

My question is what do you do with the result? I'm tracing it out as an [Object object], which is only revealing a "serverinfo" property to me. Doesn't seem to be containing the actual return data.

I tried the AS2 Remoting method of accessing the result property of the return object, but that's not defined (I'm using a remote service that I know works when using Remoting AS2-style).

Thanks!

OSCAR TRELLES

MARCH 18 2007 @ 02:57 PM

Hey Dru, good question.

When the service returns an Object (not a String or a Boolean) it actually wraps it inside one a little more complex .

The result you are expecting is actually a recorset containing a single object named 'serverInfo". If you dissect it, you will find it contains what you are looking for. This is its structure:

totalCount: The total number of records in the recordset, as a Number
initialData: The recordset data as an array
cursor: 1
serviceName: If gateway implements pageable recordsets, the name of the service that retrieves further records in the recordset
columnNames: The column names, as an array of strings.
version: 1
id: A recordset id, only used if array is recordset is paged

More info here:
http://osflash.org/documentation/amf/recordset

Y

APRIL 2 2007 @ 02:16 AM

ytfrgh

TYLER

APRIL 17 2007 @ 05:23 PM

ok so I am returning a query result from a coldfuion cfc. In as2 i would use this to acces the data in the first recocrd :

"returned.result.items[0].aDBfield"

how is it done in AS3?

JOSH

APRIL 21 2007 @ 11:24 AM

So...what happened to PendingCall? Seemed like it was a much neater way to call a PHP class w/o having to pre-build an object for the params...

B

APRIL 23 2007 @ 03:31 AM

h

DRU

APRIL 23 2007 @ 12:10 PM

So, I did a little digging, and in the process realized that the NetConnection class has always been the main class through which you do Remoting. The Remoting Components simply install a few extra classes to put a prettier face on the details. But in theory, you've always been able to do Remoting in a way similar to that outlined here, by using the NetConnection class. Oscar's RemotingService class seems like more or less the same kind of "wrapper" functionality you'd get from the Remoting classes installed by the Remoting Components download.

I'd be a bit surprised if Adobe didn't release an AS3 version of the Remoting Components, and allow us to keep using Remoting in a manner similar to the way we're used to with AS2.

LEE

APRIL 24 2007 @ 10:35 AM

I'm fairly new at this but I believe you have to deserialize the recordset, which could previously be done with the RDBMSResolver, which isn't implemented in Flash CS3.

JOSH

APRIL 27 2007 @ 03:35 PM

I wrote a deserializer for a similar wrapper, along with an in-movie NetConnection Debugger, for anyone interested in using or improving it... get the package at http://www.joshstrike.com/strike_remoting.zip Cheers!

LEE

MAY 2 2007 @ 11:32 AM

Man, you really lose alot without the RDBMSResolver & DataSet classes. Any word on anyone rewriting and optimizing these classes for AS3?

LEE

MAY 2 2007 @ 11:35 AM

To access your NetConnection result you need to loop through the result.serverInfo.initialData which is an assosiative array holding each database record. You need to tie each field to the proper result.serverInfo.columnNames, probably in the same loop function.

for (var prop in result.serverInfo.initialData) { trace(result.serverInfo.initialData[i]); }

JOSH

MAY 2 2007 @ 04:54 PM

yeah, like I said, check out my little remoting package; it does all of that.
Cheers.

LEE

MAY 2 2007 @ 05:00 PM

Ive been meaning to do that. Have you created a DeltaPacket replacement? eg a class to manage changes to the DataSet that is created from the result?

TOMMY

MAY 6 2007 @ 09:52 PM

Ok so what happened to the NetConnection Debugger when using AS3? Is there an alternative? If so, where can we get it? Thanks!

Tommy

JOSH

MAY 8 2007 @ 10:46 AM

No, nothing w/ DeltaPackets. If you do write that functionality in, please hit me up.
Tommy -- see the package.

TOMMY

MAY 8 2007 @ 07:00 PM

Excuse my ignorance but what package are you referring to? Thanks,

Tommy

TOMASZ STOCKI

MAY 11 2007 @ 06:01 AM

DRU, in file Test.php "" is missing at the beginning, then "trace" result is not "[Object object]" which its result onFault, but as in this example: "something and 2"

DRU

MAY 11 2007 @ 11:01 AM

Thanks for the tip Tomasz, but I wasn't using the php file supplied here. I was using a known-to-be-working ColdFusion CFC from another project.

JUSO

MAY 15 2007 @ 11:18 PM

could work this for web service?

JUSO

MAY 15 2007 @ 11:20 PM

Hablas espa

JONAS

MAY 24 2007 @ 08:27 AM

Juso asked how to use web services with AS3 now that the WebServiceConnector class doesn't exist

R DOLLARHIDE

MAY 30 2007 @ 10:43 AM

Thanks for the AS3 example. Not too many good ones out there that I've seen. I'm attempting to wire up your example as shown except I'm using ASP.NET 2.0. Are you familiar with how I would structure my aspx and aspx.cs files to allow calls to the server using AS3?

NICOLAS

JUNE 3 2007 @ 10:37 AM

r dollarhide:
hi! I just applied the above code (with AMF3), and am using AS3 to connect to an ASP.NET application. I'm not sure about the specifics because I am not a .NET developer in any way, but we are using Flourine as our remoting gateway between AS3 and .NET. It works well so far!

http://fluorine.thesilentgroup.com/fluorine/

J

JUNE 6 2007 @ 02:53 PM

How to call a Webservice method with Flash CS3 using AS3? Not possible is it?

KURT VAN SCHAEYBROECK

JUNE 10 2007 @ 03:12 AM

Hello,
For two days now, I'm trying to install amfphp on my OSX with Flash CS3 and Flex Builder 2.
Unfortunately, there are few clear install guies and tutorials out there for the newbies. Either OS, versions, language or details or forgotten, and so on.
This one seems better than all the rest, so I thought I write the author.
FIrst, I'm trying to call a php function from within Flash. And if that works I'll try it from within Flex.
For now, I installed amfphp 1.2.6 on OSX 10.4 in the path /Library/Webserver/Documents/amfphp/
I managed to get a php "service" or .php class in there and I can see it with the browser form amfphp.
But I can't get passed the netconnection thing.
What do I have to do when workng with CS3? DO I need to install anything?
And what do I have to put in the actionscript in my flash movie. I mean, what code do I use to actually connect?
Hopefully someone can help me.
Thanks in advance.

DOMINIQUE PERETTI

JUNE 18 2007 @ 02:16 PM

Kurt,
This is what you want to do to enable Flash Remoting in AS2 / CS3 :
http://blog.vixiom.com/2007/04/17/actionscript-20-flash-remoting-with-flash-cs3/

KURT VAN SCHAEYBROECK

JUNE 22 2007 @ 12:25 AM

Thx Dominique,
that's a helpful link. I figured it out now.