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

24 篇文章 0 订阅
以下是一个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
{

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值