flex4访问amfphp之gateway封装类

较FLEX3来说,FLEX4在remoting通讯方面做得非常好了,提供了良好的可视化操作与自动产生代码,但习惯于自己编写代码,因为个人认为这样来得更自由、同时节省代码,所以就写了一个类,配合单例形式访问,应更好一点,同时与大家分享,请指教。

封装类:

package com.zhi.helper { import flash.events.NetStatusEvent; import flash.net.NetConnection; import flash.net.Responder; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import mx.controls.Alert; public class Gateway { private var _conn:NetConnection; private var _remotingGateway:String; private var _statusMsgAry:Array=new Array(); public function Gateway() { } public function connect():void{ this._conn=new NetConnection(); _conn.connect(_remotingGateway); _conn.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);//侦听连接状态 } private function netStatusHandler(evt:NetStatusEvent):void { var info:Object = evt.info; Alert.show(info.code); switch(info.code){ case "NetConnection.Connect.Success": this._statusMsgAry.push(1); this._statusMsgAry.push("链接成功"); break; case "NetConnection.Connect.Rejected": this._statusMsgAry.push(0); this._statusMsgAry.push("远端GATEWAY连接被拒绝"); break; case "NetConnection.Connect.Failed": this._statusMsgAry.push(0); this._statusMsgAry.push("远端GATEWAY连接失败"); break; case "NetConnection.Connect.Closed": this._statusMsgAry.push(0); this._statusMsgAry.push("远端GATEWAY连接已经关闭"); break; case "NetConnection.Call.BadVersion": this._statusMsgAry.push(0); this._statusMsgAry.push("远端GATEWAY错误"); break; } } public function call(command:String,onResult:Function,onFault:Function,... arguments):void{ var responder:Responder=new Responder(function(result:Object):void{ onResult(result); },function(fault:Object):void{ onFault(fault); }); arguments.unshift(responder); arguments.unshift(command); this._conn.call.apply(this,arguments); } public function get statusMsg():Array { return _statusMsgAry; } public function get remotingGateway():String { return _remotingGateway; } public function set remotingGateway(value:String):void { _remotingGateway = value; } public function get conn():NetConnection { return _conn; } } }

单例类:

package com.zhi.singleton { import com.zhi.helper.Gateway; public class Sgateway { private static var _instance:Gateway; public function Sgateway() { if(_instance != null) { throw new Error("Singleton Error:com.zhi.singleton.Sgateway"); } } public static function getInstance():Gateway { if(_instance == null) { _instance = new Gateway(); } return _instance; } } }

你可能感兴趣的:(flex4访问amfphp之gateway封装类)