在AS3中更爽快地使用Flash Remoting

在AS3中,目前我们只能使用NetConnection来调用Flash Remoting(http://www.riafan.com/article/flash/flash-remoting-actionScript-3.0.html),不过我总觉得用起来好像不方便,参照Flash Remoting ActionScript 2.0的API,我对NetConnection调用 Remoting服务进行了简单封装,大家可以参考一下。

类似地,这套类库也包括Service、PendingCall等类,主要是主了方便已经习惯使用Flash Remoting ActionScript 2.0 API的用户。

用法很简单。

package {
	import flash.display.Sprite;
	import flash.net.ObjectEncoding;	
	import com.riafan.remoting.PendingCall;
	import com.riafan.remoting.Service;
	import com.riafan.rpc.FaultEvent;
	import com.riafan.rpc.ResultEvent;		
	/**
	 * Flash Remoting ActionScript 3.0
	 * @author Flying
	 */
	public class Main extends Sprite {
		public function Main() {
			//创建远程服务
			var service : Service = new Service("tutorials.remoting.HelloWorld", 
			"http://www.riafan.com/Gateway.aspx", 
			ObjectEncoding.AMF3);
			//调用远程方法
			var rpc : PendingCall = service.sayHello("Flying");
			//注册调用成功的事件侦听器对象
			rpc.addEventListener(ResultEvent.RESULT, successHandler);
			//注册调用成功失败的侦听器对象
			rpc.addEventListener(FaultEvent.FAULT, resultHandler);
		}
		//声明处理调用成功的侦听器函数
		private function resultHandler( re : ResultEvent ) : void {
			// 您好,Flying。这是来自Fluorine Flash remoting的问候
			trace(re.result);
		}
		//声明处理调用失败的侦听器函数
		private function faultHandler( fe : FaultEvent ) : void {
			trace(fe.fault.description);
		}
	}
}

是不是方便多了?点击此处下载源码。

评论: 1 | 引用: 0 | 查看次数: -
引用Fenny[2008-04-12 09:57 AM | 无网站 | 无Mail | 58.56.19.36 | 删除 | 取消审核 | 回复回复]
[正在加载评论信息,请稍候...]
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭