package aw.net.amf{ import flash.events.NetStatusEvent; import flash.net.NetConnection; import flash.net.ObjectEncoding; import flash.net.Responder; import flash.utils.Dictionary; public class ServiceCaller extends Object{ static private const callers:Dictionary = new Dictionary(); static private var _connection:NetConnection; static public var gatewayURL:String; static public var objectEncoding:uint = ObjectEncoding.AMF3; protected var _method:String; protected var _responder:Responder; protected var _successHandler:Function; protected var _errorHandler:Function; public function ServiceCaller(method:String, successHandler:Function, errorHandler:Function=null):void{ super(); _method = method; _responder = new Responder(callSuccessHandler, callErrorHandler); _successHandler = successHandler; _errorHandler = errorHandler; if(!_connection){ initConnection(); } } public function call(...args):void{ this.callWithArgumentList(args); } public function callWithArgumentList(args:Array):void{ var list:Array = [this._method, this._responder]; _connection.call.apply(_connection, list.concat(args)); callers[this] = true; } protected function callSuccessHandler(value:*):void{ delete callers[this]; this._successHandler(value); } protected function callErrorHandler(value:*):void{ delete callers[this]; if(this._errorHandler!=null){ this._errorHandler(value); }else{ trace(' -- error:', this._method, value); } } static public function get connection():NetConnection{ return _connection; } static public function initConnection():void{ var nc:NetConnection = new NetConnection(); nc.addEventListener(NetStatusEvent.NET_STATUS, сonnectionStatusHandler); nc.objectEncoding = objectEncoding; nc.connect(gatewayURL); nc.client = {}; nc.client.onBWDone = onBWDoneHandler; _connection = nc; } static private function onBWDoneHandler(...args:Array):void{} static private function errorNetStatus(event:NetStatusEvent):void{ switch(event.info.code){ case "NetConnection.Connect.InvalidApp": case "NetConnection.Connect.AppShutdown": case "NetConnection.Connect.Rejected": case "NetConnection.Connect.Failed": _connection.connect(gatewayURL); break; } } static private function сonnectionStatusHandler(event:NetStatusEvent):void{ //trace(event.info.code); if(event.info.code == 'NetConnection.Call.Failed'){ trace(" -- call failed"); } } } }