使用RemoteObject调用后台服务

在我的“flex+blazeDS+java示例” http://yiyu.iteye.com/admin/blogs/549728中,调用后台服务是在mxml文件中定义<mx:RemoteObject>实现的,后来发现其实可以不使用<mx:RemoteObject>,而是在ActionScript代码中使用mx.rpc.remoting.RemoteObject实现。

以下就是相关的代码:

package test.client.model
{
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.Operation;
	import mx.rpc.remoting.RemoteObject;
	
	public class UserModel
	{
		const ENDPOINT : String = "http://localhost:8080/test-server/messagebroker/amf";
		var ro : RemoteObject;
		
		public function UserModel()
		{
			ro = new RemoteObject();
			ro.destination = "echoServiceDestination";
			var c : AMFChannel = new AMFChannel(null, ENDPOINT);
			var cs : ChannelSet = new ChannelSet();
			cs.addChannel(c);
			ro.channelSet = cs;
		}

		public function isUserValid(username:String, password:String):void
		{
			var op : Operation = Operation(ro.getOperation("echo"));
			op.addEventListener(ResultEvent.RESULT, resultOfEcho);
			op.addEventListener(FaultEvent.FAULT, faultOfEcho);
			op.send(username + " " + password);
		}
		
		public function resultOfEcho(event:ResultEvent):void {
			trace(event.result);
		}
		
		public function faultOfEcho(event:FaultEvent):void {
			trace(event.fault);
		}
	}
}

你可能感兴趣的:(C++,c,jsp,Flex,actionscript)