Flash Remoting AS2 vs AS3

One of the new things that was changed in Flash CS3/Actionscript3.0 was the way that flash remoting is handled. In AS2, You needed to create a pendingCall to your service’s method(s) and access them that way. In AS3, you make a direct call to the service’s method.

For details, and code examples on the changes, keep reading.
In actionscript2.0 land, the code that you would normally use to make a Flash remoting connection would look something like this-


NetDebug.initialize();
var myResponder:RelayResponder = new RelayResponder(this,"onResult","onFault");
var myService:Service = new Service("http://127.0.0.1/flashservices/gateway.php",null,"HelloWorld",null,myResponder);
var temp_pc:PendingCall = myService.("Hello World!");

Now, in AS3.0, things have changed a little bit. Using Flash CS3, your code would look something like this, for a generic remoting example-


import flash.net.*; //Required for all the netConnection classes
var myService:NetConnection = new NetConnection();
myService.objectEncoding = ObjectEncoding.AMF0;
myService.connect("http://127.0.0.1:8500/flashservices/gateway/");
var responder = new Responder(onResult,onFault);
var var1:String = "variable1";
myService.call("mySiteName.cfcName.methodName",responder,var1,var2,var3,var4,etc..etc..);

One of the most important lines is the last one. The actual CALL to your method. In order to pass variables to the remoting connection, you just append them at the end of the call string like I did above.

8 comments so far

  1. Pablo on

    Hi threre!

    Could you show me an example regarding:

    ”mySiteName.cfcName.methodName”

    What does mySiteName and cfcName ref. to?

    Is mysiteName the name of the aspx file/page? and cfcName the name of the class inside the page?

    Kind regads,
    Pablo

  2. curiousmindsmedia on

    Hey Pablo,

    “mySiteName” is the folder on your site where the .cfc file is located. This is relative to the url you passed in via the “myService.connect(“http://yoursite”)” method earlier.

    “cfcName” is the name of this .cfc file.

    “methodName” refers to the name of the function you want to hit within this .cfc file.

    Hopefully this clears it up, let us know if you have any other questions.

    Best,
    CMM

  3. Pablo on

    Hi again…sorry…but I’m trying to use this with asp.net 2.0 – is that possible??

    I don’t have a .cfc file – only swf and aspx files…

    I have tried with:
    http://amfnet.openmymind.net/howto/default.aspx

    But without luck ;( Can you help me? They do not show how to call a server method c# method…

    Pablo

  4. curiousmindsmedia on

    Pablo, can you post up the url and details of your webservice and fla?

  5. John Barrett on

    Hi,
    Can I ask about how to trace my cfc function?
    I asked the question on flashkit(and else where):
    http://board.flashkit.com/board/showthread.php?t=778121

    I am trying to trace what the cfc returns, such as:
    [INFO] : Creating Service for cfcs.getTest
    [INFO] : Creating gateway connection for http://localhost/flashservices/gateway
    [INFO] : Successfully created Service
    [INFO] : Invoking getTestConn on cfcs.getTest
    [INFO] : cfcs.getTest.getTestConn() returned “….connection successful”

    I have been struggling with this for a month:( How can I trace the above using actionscript 3? It seems like in your example that there is no longer the “pending call”? I am going to try your solution, and stoked to find your blog`-`

    —- my actionscript———-
    import flash.net.*;

    var myservice:NetConnection = new NetConnection();
    myservice.connect(“http://localhost/flashservices/gateway”);

    var responder:Responder = new Responder(onResult, onFault);

    function onResult(responder:Object):void{
    for(var i in responder ){
    trace(responder[i]);
    }
    }

    function onFault(responder:Object):void{
    for(var i in responder ){
    trace(responder[i]);
    }

    }

    myservice.call(“cfc.getTestConn.getTestConn”, responder);

    Thanks so much for any help`-`
    Johnny

  6. Elliot Rock on

    Johnny,

    I am puzzled to where your event listeners are to drive these functions with their traces?

    The answer maybe in the events for responder.

    E

  7. Tommy on

    The event listeners are in this line:

    var responder = new Responder(onResult,onFault);

    If the remote site responds with data, the function onResult will be called. And if there is an error reported, onFault will be called. If the remote site doesn’t respond, maybe nothing wil happen. Johnny didn’t show code for the two functions onResult and onFault, but they look something like http://www.flash-db.com/Tutorials/loadingAS3/loadingData.php?page=6

  8. amf0 vs amf3 on

    Hello, I notice that you use the AMF0 object encoding rather than AMF3 encoding.

    Is your server gateway to unsupport the AMF3 or there are other reason?


Leave a reply