This is a technical post meant for ActionScript developers.
Flash CS3 and ActionScript 3 projects have no means (by default) of using flash remoting. Since we, at Rhinofly, do projects in Flex, Flash CS3 and pure ActionScript 3 we needed remoting to function in all of the project types. I was given the task to find out how we could use AMF 3 remoting within Flash CS3 and ActionScript 3 projects.
Note that this has only been tested with ColdFusion 7.02 as backend.
Doing a call
Three imporant classes needed for communication are:
mx.messaging.messages.RemotingMessage
mx.messaging.messages.AcknowledgeMessag
mx.messaging.messages.ErrorMessage
These can be found in the ${FRAMEWORKS}\libs\rpc.swc package, so make sure you add this package to your project.
To make a call you can use the following code:
//register the correct class aliases
registerClassAlias("flex.messaging.messages.RemotingMessage", RemotingMessage);
registerClassAlias("flex.messaging.messages.AcknowledgeMessage", AcknowledgeMessage);
registerClassAlias("flex.messaging.messages.ErrorMessage", ErrorMessage);
//create a netconnectionvar nc:NetConnection = new NetConnection();
nc.connect("http://www.domain.com/flex2gateway/");
//create the arguments that will be sent to the method
var methodArguments:Array = new Array();
//create a remoting message
var remotingMessage:RemotingMessage = new RemotingMessage();
remotingMessage.operation = "methodToCall";
remotingMessage.source = "CFCToCall";
remotingMessage.body = methodArguments;
//The values of these 2 values come from the services-config.xml which is located in the ColfFusion server
remotingMessage.destination = "ColdFusion";
remotingMessage.headers = {DSEndpoint: "my-cfamf"};
//create a responder
var responder:Responder = new Responder(result, fault);
//make the call
nc.call(null, responder, remotingMessage);
You will most likely get the following error:
ArgumentError: Error #2004: One of the parameters is invalid.
This problem can be solved in 2 ways:
1. Turn of debugging on the ColdFusion server
2. Read on
Within the debugging information sent back to the flash client there is an object typed as ArrayCollection. In order to use the ArrayCollection in our project we need to add ${FRAMEWORKS}/libs/flex.swc to our project. Use the following code to register the class:
registerClassAlias("flex.messaging.io.ArrayCollection", ArrayCollection);
The ArrayCollection is depends on a resource bundle so we need to add ${FRAMEWORKS}\locale\{locale} aswell.
Handling the response
The response methods should look like this.
function result(result:AcknowledgeMessage):void
{
//result.body contains the actual data
};
function fault(result:ErrorMessage):void
{
};
I hope this helps any developers out there.
The following error can be fixed with the line of code below:
//No class registered for interface ‘mx.resources::IResourceManager’.
Erik op 30 juli 2008 om 15:49Singleton.registerClass(“mx.resources::IResourceManager”, ResourceManagerImpl);
Do not forget to add the following imports:
import mx.core.Singleton;
Erik op 30 juli 2008 om 15:50import mx.resources.ResourceManagerImpl;
Can you provide a fla template that I can open up in Flash CS3 Pro ?
mores op 24 december 2008 om 20:56[...] (814) 3. Registreer nu je windows live adres (666) 4. 3d avatars die reageren op beweging (602) 5. AMF3 flash remoting in flash cs3 and actionscript 3.0 (534) 6. De 20 beste albums van 2008 (465) 7. Plazoo houdt er vreemde verkooptechnieken op na (454) [...]
Frank-ly » Blog Archive » Als 2008 langzaam aan 2009 wordt… op 30 december 2008 om 12:24http://www.frank-ly.nl/als-2008-langzaam-aan-2009-wordt
[...] flex classes amd you can use them in a pure Actionscript project. There are two resources i found, here and here, which helped me finding two alternative solutions, one resulting in a 82 KiloByte .swf [...]
Connecting Flash and Javascript via BlazeDS and DWR – 1/2 « Ghost23 Blog op 16 augustus 2009 om 15:11http://www.ghost23.de/blog/2008/10/connecting-flash-and-javascript-via-blazeds-and-dwr-12/
Thanks! Very helpful.
Hugo op 23 november 2009 om 16:24http://www.hdinteraction.nl/blog