Call AMF RPC methods from Flash

If you would like consume some services through RemoteObject from Flash in the same fashion you do in Flex, you will need to follow these step:
  • Add the rpc.swc and framework.swc from your {FLEX_HOME}/frameworks/libs to you ptoject or .fla classpath

  • Optional: If you are compiling an Actionscript Project in Flash Builder you'll also need the resource bundles for those swcs. So add also the rpc_rb.swc and framework_rb.swc from {FLEX_HOME}/frameworks/locale to your classpath.

  • Look at the code below, the initializeRPC and registerClasses methods are what you need to make things work.


You are now ready to call remote methods via amf like this simple example.
Notes:
You will bring nearly 100kb of footprint to your swf due to dependencies from the flex framework. You reduce that footprint by registering a custom collection class instead of the ArrayCollection. The only requirement is that your collection class implements the IExternalizable interface.
import mx.collections.ArrayCollection;
import mx.core.mx_internal;
import mx.logging.Log;
import mx.logging.targets.TraceTarget;
import mx.messaging.ChannelSet;
import mx.messaging.channels.AMFChannel;
import mx.messaging.config.ConfigMap;
import mx.messaging.config.LoaderConfig;
import mx.messaging.messages.AcknowledgeMessage;
import mx.messaging.messages.AcknowledgeMessageExt;
import mx.messaging.messages.CommandMessage;
import mx.messaging.messages.CommandMessageExt;
import mx.messaging.messages.ErrorMessage;
import mx.messaging.messages.RemotingMessage;
import mx.rpc.AbstractOperation;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.RemoteObject;
import mx.utils.ObjectProxy;

use namespace mx_internal;

const ID:String = "my-amf";
const ENDPOINT:String = "http://your.domain/amf/gateway";
const DEFAULT_SOURCE:String = "your.service";
const DEFAULT_SOURCE:String = "your.service";

var remoteObject:RemoteObject;

/**
* This initializes the rpc library.
* After this you should see the requests going through.
* Note: LoaderConfig won't show up in the code hints, so you have to write down the import manually.
*/
function initializeRPC():void {

LoaderConfig.mx_internal::_url = loaderInfo.url;
LoaderConfig.mx_internal::_parameters = loaderInfo.parameters;
}

/**
* We register a bunch of class aliases that are needed by the rpc methods.
* The list *may be* incomplete, if you get some TypeCoertion error try registering that class.
*/
function registerClasses():void {

registerClassAlias("flex.messaging.messages.ErrorMessage", ErrorMessage);
registerClassAlias("flex.messaging.messages.CommandMessage", CommandMessage);
registerClassAlias("flex.messaging.messages.RemotingMessage", RemotingMessage);
registerClassAlias("flex.messaging.messages.AcknowledgeMessage", AcknowledgeMessage);
registerClassAlias("DSC", CommandMessageExt);
registerClassAlias("DSK", AcknowledgeMessageExt);
registerClassAlias("flex.messaging.config.ConfigMap", ConfigMap);
registerClassAlias("flex.messaging.io.ObjectProxy", ObjectProxy);
registerClassAlias("flex.messaging.io.ArrayCollection", ArrayCollection);
}

/**
* Prepare the RemoteObject, creating the appropriate AMF channel
* and setting up the default event handlers.
*/
function prepareDefaultRemoteObject():void {

var channel:AMFChannel = new AMFChannel(ID, ENDPOINT);

var channelSet:ChannelSet = new ChannelSet();
channelSet.addChannel(channel);

remoteObject = new RemoteObject();
remoteObject.source = DEFAULT_SOURCE;
remoteObject.destination = DEFAULT_DESTINATION;
remoteObject.channelSet = channelSet;
remoteObject.addEventListener(FaultEvent.FAULT, onDefaultServiceFault);
remoteObject.addEventListener(ResultEvent.RESULT, onDefaultServiceResult);
}

/**
* Create and execute the remote method.
*/
function makeAnonymousRemoteCall():void {

var operation:AbstractOperation = ro.getOperation("getProduct");
operation.send();
}


/**
* Handles faulty replies from the remote methods.
*/
function onDefaultServiceFault(event:FaultEvent):void {

trace(event.fault.faultDetail);
}

/**
* Handles successful replies from the remote methods.
*/
function onDefaultServiceResult(event:ResultEvent):void {

trace(event.result.toString());
}

// All RPC classes use the Flex logging API so enabling it in the Flash project could be helpful.
Log.addTarget(new TraceTarget());

initializeRPC();
registerClasses();

prepareDefaultRemoteObject();
makeAnonymousRemoteCall();

Comments on this post

That's not just the best answer. It's the betsset answer!
Grade A stuff. I'm uqnuestoianbly in your debt.
QUJ7do , gnxzxkmtdssh, [link=http://fpivckkxjocf.com/]fpivckkxjocf[/link], http://steaohrengnn.com/
The purchases I make are entirely based on these articels.
home insurance =-) florida home owners insurance 275
Cialis 605289 nexium 950
cheap health insurance cvwz car insurance rates abuqx
health insurance nfq car insurance quotes 106
car insurance 7720 cheap car insurance 0462
whole life insurance 10625 cheap auto insurance ioebnq
cheap health insurance 4039 homeowner s insurance :]]
life insurance quotes :]]] home insurance >:-)))
permanent life insurance 8-P car insurance %PP
auto insurance quotes =-P car insurance quotes btu
american home insurance gept car insurance quotes 45938
seroquel skwu propecia cyxp
These aren't the mom's footwear and perhaps they are not your own tiny sister's. These sneakers are only to the woman which does not need designer shoes for women

Thanks for your thoughts. It's helepd me a lot.
GCXayF <a href="http://fmgcnxigmpth.com/">fmgcnxigmpth</a>
<a href="http://www.worldofdrugs.net/">provigil</a> 755 <a href="http://www.painrelieveronline.com/">are ultram pain pills addictive</a> 005 <a href="http://www.pillsupport.net/">toradol pills</a> =-)))
In recent years Louis Vuitton Handbagsthis inland port city of nearly 300,000Louis Vuitton Handbags people has earned several distinctions, none Chanel Handbagsof them good. Twice atop Forbes'Coach Factory Outlet list of America's Most Miserable Louis Vuitton PursesCities ... Second highest violent-crimeChanel Handbag rate in California ... Second highest home-foreclosure rate Boss Beltsof all major U.S. metro areas. Now, Stockton is on the Coach Factory Onlineverge of another dubious benchmark: bankruptcy.Louis Vuitton Purses In its third straight year of fiscal emergency,Coach Factory Online the city faces a deficit of as high asCoach Factory Outlet $38 million on its $165 million general fundCoach Factory Stores budget. As required by state law,Coach Outlet Store the city
I feel so much hpaiper now I understand all this. Thanks!
VhBNMY <a href="http://udpctznxszny.com/">udpctznxszny</a>
Christophe Coenraets Rich Internet Applications, Flex, AIR, Java, Android Skip to content BioAIR SamplesAIR Web ServerAIR Application with Embedded TomcatAIR for Android SamplesEmployee Directory on AIR for AndroidVoice Notes for AndroidReal Time Trader Desktop for AndroidEmployee Directory Android Market App Companion PageAndroid SamplesTutorial: Building an Android Application in 6 Steps Tutorial: Flex for Android in 90 Minutes
People will regret trintusg this site with their info.. it's already happening actually.. i've heard it twice within 2 months in my own little community, people are being investigated, getting a bad rep, losing jobs due to their status on FB or for a comment they post. NOTHING online is private guys ESpecially FB
This is one of those things you search for in the first days of using flex builder if you’re not a Java guy…. I really wish the flex builder team would spend a week learning a .NET language on visual studio. Visual Studio really is the ONLY thing that microsoft has ever gotten right. The energy company I work for has been migrating to Flex for the last six months — Because of corporate structure, and the fact that everybody here uses visual studio, I can foresee a switch to Silverlight, even after the $million+ that has been invested in our custom Flex framework.

<a href="http://www.edpackages.com/">levitra</a> <a href="http://www.comparemedsprices.net/">acyclovir</a>
People will regret trusting this site with their info.. it’s already happening actually.. i’ve heard it twice within 2 months in my own little community, people are being investigated, getting a bad rep, losing jobs due to their “status” on FB or for a comment they post. NOTHING online is private guys ESpecially FB

<a href="http://www.autoinsuroptions.com/">insurance auto auctions</a> <a href="http://www.autocoveragerates.net/">cheap california auto insurance</a>

Post a comment

Embed any code between [code] [/code] tags
captcha image   Reload