论坛首页 编程语言技术论坛

动态配置AMF与后台接口调用

浏览 2375 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-09-28   最后修改:2011-09-28
以下是一个AMF调用类:
package common
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	import mx.controls.Alert;
	import mx.core.FlexGlobals;
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.rpc.AsyncToken;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	
	public class SimpleAmf
	{
		public function SimpleAmf(v:SingletonTemp,sourceData:String,url:String=null ){
			_dispatcher = new EventDispatcher();
			_service = new mx.rpc.remoting.RemoteObject();
			
			_nonPollingChannelSet = new mx.messaging.ChannelSet();		
			_pendingOperations = new flash.utils.Dictionary();
			_cachedOperations = new Array();
			_serviceUrl = url;
			_sourceData = sourceData;
			
			initChannels();
			
			_service.addEventListener(mx.rpc.events.FaultEvent.FAULT, serviceFaultHandler);
		}
		
		/**
		 * destination初始化
		 */
		protected function initChannels():void{
			_service.requestTimeout = _requestTimeout;
			_service.source = _sourceData;//"ssoc.AssetService";
			_service.destination = "amfphp";
			_service.source= _sourceData;
			_service.endpoint = _serviceUrl;
		}
		
		/**
		 * SimpleAmf单例模式
		 * */
		public static function instance(source:String,url:String=null):SimpleAmf{
			
			if ( url == null ){
				url = FlexGlobals.topLevelApplication._host;//要传入的gateway从全局变量中取,如:http://192.168.1.146/ssoc/phpserver/gateway.php
			}
			var key:String = url + "/" + source; //md5.hash( _binKey ).toString();
			if( null == _instanceHash[key] ){
				var server:SimpleAmf = new SimpleAmf(new SingletonTemp(),source,url);
				_instanceHash[key] = server;//把调用的后台接口类存入数组,这样第二次就可直接调用了
			}
			
			return _instanceHash[key];
		}
		
		/**
		 * 
		 * @param Operation
		 * @param eventType
		 * @param arguments
		 * @param bRecallOnError
		 * 
		 */
		protected function startRemoteOperation(Operation:String, eventType:String, arguments:Object,callback:Function=null, bRecallOnError:Boolean=true):void{
			var requestData:AmfRequestData = new AmfRequestData(eventType);
			requestData.operation = _service.getOperation(Operation);
			requestData.callback = callback;
			requestData.recallOnError = bRecallOnError;
			
			requestData.operation.arguments = arguments;
			requestData.token = requestData.operation.send();
			
			_pendingOperations[requestData.token] = requestData;
			return;
		}
		
		/**
		 * 
		 * @param token
		 * @param arg2
		 * @return 
		 * 
		 */        
		protected function finishRemoteOperation(token:mx.rpc.AsyncToken, arg2:Boolean=true):AmfRequestData{
			var requestData:*;
			requestData = this._pendingOperations[token] as AmfRequestData;
			if (requestData == null)
			{
				return null;
			}
			delete _pendingOperations[token];
			
			return requestData;
		}
		
		/**
		 * callback(success:Boolean,response:Object,arguments:Object)
		 * @success 是否调用城功
		 * @response 返回的数据
		 * @arguments 传入的调用参数
		 * */
		private function onDefaultGetHandler(resultEvent:mx.rpc.events.ResultEvent):void{	
			var requestData:AmfRequestData = finishRemoteOperation(resultEvent.token);	
			if ( requestData.callback != null ){
				requestData.callback(true,resultEvent.result,requestData.operation.arguments);
			}			
		}
		
		/**
		 * 异常处理
		 */
		private function serviceFaultHandler(event:FaultEvent):void{
			var requestData:AmfRequestData = finishRemoteOperation(event.token);	
			if ( requestData.callback != null ){
				requestData.callback(false,event.fault.faultString,requestData.operation.arguments);				
			}
		}
		
		/**
		 * 请求调用函数
		 */
		public function request( operation:String, arguments:Object,callback:Function=null, eventType:String=null ):void{
			
			_service.getOperation(operation).addEventListener(mx.rpc.events.ResultEvent.RESULT, onDefaultGetHandler);			
			
			startRemoteOperation( operation, eventType , arguments,callback );
		}
		
		protected var _pendingOperations:flash.utils.Dictionary;
		protected var _service:mx.rpc.remoting.RemoteObject;
		protected var _requestTimeout:int=120;
		protected var _dispatcher:EventDispatcher;
		protected var _cachedOperations:Array;
		protected var _nonPollingChannelSet:mx.messaging.ChannelSet;
		private var _serviceUrl:String=null;
		private var _sourceData:String=null;
		
		private static var _instanceHash:Dictionary = new Dictionary();		
	}
}

class AmfRequestData{
	
	public function AmfRequestData(_eventType:String=null){
		eventType = _eventType;
		return;
	}
	
	public var callback:Function=null;	
	public var eventType:String=null;	
	public var operation:mx.rpc.AbstractOperation=null;	
	public var recallOnError:Boolean=false;	
	public var token:mx.rpc.AsyncToken=null;
}

class SingletonTemp{};


调用方法:
/**  
 * @SsocService 后台接口调用类  
 * @getp_asset_eventCount 调用接口  
 * @[bTime,eTime,"I",ip]调用接口传入参数,必须放到数组中  
 * @getp_asset_eventCountHandler 回调函数名  
 */  
SimpleAmf.instance("SsocService").request("getp_asset_eventCount",[bTime,eTime,"I",ip],getp_asset_eventCountHandler);  

回调函数:
/**  
 * callback(success:Boolean,response:Object,arguments:Object)  
 * @success 是否调用城功  
 * @response 返回的数据  
 * @arguments 传入的调用参数  
 * */  
private function getp_asset_eventCountHandler(success:Boolean,response:Object,arguments:Object):void  
{  
                  
}  
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics