There are alot of ways to connect to a remote service in Flex / Flash.
You can do it tag based:
Actionscript:
-
<mx:Application
-
xmlns:mx="http://www.adobe.com/2006/mxml"
-
layout="absolute">
-
-
<mx:RemoteObject id="blazeService" fault="onFault(event)" source="hello.Hello" endpoint="http://localhost:8400/hello/messagebroker/amf/" destination="Hello">
-
<mx:method name="sayHello" result="onResult(event)" />
-
</mx:RemoteObject>
-
-
<mx:Script>
-
-
<![CDATA[
-
import mx.messaging.channels.AMFChannel;
-
import mx.rpc.events.ResultEvent;
-
import mx.rpc.events.FaultEvent;
-
-
private function onResult(e:ResultEvent):void {
-
trace("Result" + e.result);
-
}
-
-
private function onFault(e:FaultEvent):void {
-
trace("fault");
-
}
-
]]>
-
</mx:Script>
-
-
</mx:Application>
or actionsript based:
Actionscript:
-
<?xml version="1.0" encoding="utf-8"?>
-
<mx:Application
-
xmlns:mx="http://www.adobe.com/2006/mxml"
-
layout="absolute"
-
creationComplete="onCreationComplete()">
-
-
<mx:Script>
-
<![CDATA[
-
import mx.rpc.events.FaultEvent;
-
import mx.rpc.events.ResultEvent;
-
import mx.messaging.ChannelSet;
-
import mx.messaging.channels.AMFChannel;
-
import mx.rpc.remoting.RemoteObject;
-
-
private var ro:RemoteObject;
-
private function onCreationComplete():void {
-
-
var channelSet:ChannelSet = new ChannelSet();
-
var amfChannel:AMFChannel = new AMFChannel();
-
ro = new RemoteObject();
-
-
ro.addEventListener(ResultEvent.RESULT, onResult);
-
ro.addEventListener(FaultEvent.FAULT, onFault);
-
-
amfChannel.uri = "http://localhost:8400/hello/messagebroker/amf/";
-
channelSet.channels = [amfChannel];
-
-
ro.channelSet = channelSet;
-
ro.source = "hello.Hello";
-
ro.destination = "Hello";
-
ro.getOperation("sayHello").send();
-
-
}
-
-
private function onResult(e:ResultEvent):void {
-
trace("Result" + e.result);
-
}
-
-
private function onFault(e:FaultEvent):void {
-
trace("fault");
-
}
-
-
]]>
-
</mx:Script>
-
-
</mx:Application>
But eventually, you'll be required to re-use the code. There are a lot of ways to do this. You may extend the RemoteObject Class or wrap it in another class. It's up to you to decide which way you want. Anyway..
Here's the wrapper class.
RemoteObjectWrapper.as
Actionscript:
-
package {
-
-
import flash.events.EventDispatcher;
-
-
import mx.messaging.ChannelSet;
-
import mx.messaging.channels.AMFChannel;
-
import mx.rpc.events.FaultEvent;
-
import mx.rpc.events.ResultEvent;
-
import mx.rpc.remoting.RemoteObject;
-
-
public class RemoteObjectWrapper extends EventDispatcher {
-
-
public function RemoteObjectWrapper (endpoint:String = null) {
-
super();
-
if(endpoint) {
-
_endpoint = endpoint;
-
initializeRemoteObject();
-
}
-
}
-
-
private var remoting:RemoteObject;
-
protected function initializeRemoteObject():void {
-
try {
-
remoting = new RemoteObject();
-
remoting.addEventListener(ResultEvent.RESULT, onResult);
-
remoting.addEventListener(FaultEvent.FAULT, onFault);
-
-
var channelSet:ChannelSet = new ChannelSet();
-
var amfChannel:AMFChannel = new AMFChannel();
-
amfChannel.uri = _endpoint;
-
channelSet.channels = [amfChannel];
-
remoting.channelSet = channelSet;
-
remoting.source = _source;
-
remoting.destination = _destination;
-
}catch(e:Error) {}
-
}
-
-
public function send(method:String, param:Object = null):void {
-
if(!remoting) {
-
initializeRemoteObject();
-
}
-
if(param) {
-
remoting.getOperation(method).send(param);
-
}else {
-
remoting.getOperation(method).send();
-
}
-
}
-
-
private function onResult(result:ResultEvent):void {
-
dispatchEvent(new RemoteObjectWrapperEvent(RemoteObjectWrapperEvent.RESULT, result.result));
-
}
-
-
private function onFault(fault:FaultEvent):void {
-
dispatchEvent(new RemoteObjectWrapperEvent(RemoteObjectWrapperEvent.FAULT, fault.fault));
-
}
-
-
private var _endpoint:String;
-
public function set endpoint(_g:String):void {
-
_endpoint = _g;
-
}
-
-
public function get endpoint ():String {
-
return _endpoint;
-
}
-
-
private var _destination:String;
-
public function set destination(_g:String):void {
-
_destination = _g;
-
}
-
-
public function get destination ():String {
-
return _destination;
-
}
-
-
private var _source:String;
-
public function set source(_g:String):void {
-
_source = _g;
-
}
-
-
public function get source ():String {
-
return _source;
-
}
-
-
}
-
};
and the event:
RemoteObjectWrapperEvent.as
Actionscript:
-
package {
-
-
import flash.events.Event;
-
-
public class RemoteObjectWrapperEvent extends Event {
-
-
public static const RESULT:String = "result";
-
public static const FAULT:String = "fault";
-
-
public var data:Object = new Object();
-
-
public function RemoteObjectWrapperEvent (type:String, _data:Object){
-
super(type);
-
data = _data;
-
}
-
-
override public function clone():Event {
-
return new RemoteObjectWrapperEvent(type, data);
-
}
-
-
}
-
-
}
sample usage:
Actionscript:
-
<?xml version="1.0" encoding="utf-8"?>
-
<mx:Application
-
xmlns:mx="http://www.adobe.com/2006/mxml"
-
creationComplete="onCreationComplete()"
-
layout="absolute">
-
-
<mx:Script>
-
<![CDATA[
-
import RemoteObjectWrapperEvent;
-
import RemoteObjectWrapper;
-
-
private var ro:RemoteObjectWrapper;
-
private function onCreationComplete():void {
-
ro = new RemoteObjectWrapper();
-
ro.addEventListener(RemoteObjectWrapperEvent.RESULT, onResult);
-
ro.addEventListener(RemoteObjectWrapperEvent.FAULT, onFault);
-
ro.endpoint = "http://localhost:8400/hello/messagebroker/amf/";
-
ro.source = "hello.Hello";
-
ro.destination = "Hello";
-
-
ro.send("sayHello");
-
-
}
-
-
private function onResult(e:RemoteObjectWrapperEvent):void {
-
trace("Result" + e.data);
-
}
-
-
private function onFault(e:RemoteObjectWrapperEvent):void {
-
trace("fault");
-
}
-
-
]]>
-
</mx:Script>
-
-
</mx:Application>
So, I think you see the difference. For me, having a wrapper class makes the code a lot more easy to understand, will lessen errors that might occur, and the nicest thing is you have a code that is easy to maintain.
If you don't want to add compiler options, this is the also one way to do it.
~ Ashier



April 28th, 2008 at 12:52 am
Hi Ashier,
Unless I’m missing something, RemoteObject is Flex-only. I can’t believe that it’s not usable via pure AS3 and/or Flash 9. What were they thinking?
Thank you for your support of Singularity, by the way
Aral
April 28th, 2008 at 7:13 am
Ooops…
Thanks
July 3rd, 2008 at 11:19 pm
Works! Thanks
August 2nd, 2008 at 7:39 am
Great Thanks for this post!
August 24th, 2008 at 1:35 am
Thanks for the post - actually you can get RemoteObject working in AS3. I documented it here:
http://yaa-blog.blogspot.com/2008/08/remoting-with-blazeds-from-plain.html
August 15th, 2009 at 7:59 am
Thank’s worked very nice with Singlethon Class.