[纯AS的RemoteAMF3]
目前支持:AMFPHP,CFC,Fluorine,LCDS
一.怎么使用
//指明amfURL
var amfChannelUri:String="http://192.168.1.102/CompanyWebSite/amfphp/gateway.php";
//AMF服务
var AMFServer:String="amfphp"
//单例,包地址,amfURL
var MyAMFDM:AMFDataManager=AMFDataManager.getDataManager(AMFServer,"website.admin.LoginPage",amfChannelUri)
//执行方法名,有参数可带参数
MyAMFDM.makeRemoteCall("doLogin","帝鼎","zx8974sczzez21dd")
//侦听
MyAMFDM.addEventListener(ResultEvent.RESULT, myResultHandler);
MyAMFDM.addEventListener(FaultEvent.FAULT, handleFault);
二.简单例子
1.整合FluorineFX的例子:
MXML:(注,这是在Flex4 SDK下编译的)
<?xml version="1.0" encoding="utf-8"?>
<Application xmlns="http://ns.adobe.com/mxml/2009"
layout="absolute"
creationComplete="DDinit();">
<Script source="AppAS/FluorineFX.as"/>
</Application>
FluorineFX.as:
import mx.collections.ArrayCollection;
import mx.events.ResizeEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import net.diding.managers.AMFDataManager;
private var IDArray:Array;
private var userNameArray:Array;
private var userPWDArray:Array;
private var items:ArrayCollection
/************************************************
* FluorineFX
* **********************************************/
private function DDinit():void{
var amfChannelUri:String="http://192.168.1.102/MSWeb/Gateway.aspx";
var AMFServer:String="fluorine"
var MyAMFDM:AMFDataManager=AMFDataManager.getDataManager(AMFServer,"net.diding.CheckUser",amfChannelUri)
MyAMFDM.makeRemoteCall("GetData");
MyAMFDM.addEventListener(ResultEvent.RESULT, myResultHandler);
MyAMFDM.addEventListener(FaultEvent.FAULT, handleFault);
}
private function myResultHandler(rs:ResultEvent):void{
trace(rs.result)
var re:Array=rs.result.tables.serverInfo.initialData as Array;
var TableInfoLength:Number=rs.result.tables.serverInfo.columnNames.length;
var cant:Number = rs.result.tables.serverInfo.totalCount;
IDArray=[];
userNameArray=[];
userPWDArray=[]
for(var i:int=0; i<re.length;i++){
for(var j:int=0;j<TableInfoLength;j++){
var columName:String=rs.result.tables.serverInfo.columnNames[j];
switch(columName){
case "id":
IDArray.push(re[i][j]);
break;
case "userName":
userNameArray.push(re[i][j]);
break;
case "userPWD":
userPWDArray.push(re[i][j]);
break;
}
}
}
ShowList(IDArray,userNameArray,userPWDArray)
}
private function ShowList(IDArray:Array,userNameArray:Array,userPWDArray:Array):void{
items = new ArrayCollection();
items.removeAll();
for(var i:int =0;i<IDArray.length;i++)
{
var obj:Object = new Object();
obj.id = IDArray[i];
obj.userName=userNameArray[i];
obj.userPWD=userPWDArray[i];
items.addItem(obj);
}
//MyDataDG.dataProvider=items;
}
private function handleFault(event:FaultEvent):void{
trace("error==="+event)
}
2.整合AMFPHP1.9的例子:
MXML:同上,只需要改变
<Script source="AppAS/AMFPHP1.9.as"/>即可
AMFPHP1.9.as
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import net.diding.managers.AMFDataManager;
private function DDinit():void{
/************************************************
* AMFPHP1.9
* **********************************************/
var amfChannelUri:String="http://192.168.1.102/CompanyWebSite/amfphp/gateway.php";
var AMFServer:String="amfphp"
var MyAMFDM:AMFDataManager=AMFDataManager.getDataManager(AMFServer,"website.admin.LoginPage",amfChannelUri)
MyAMFDM.makeRemoteCall("doLogin","帝鼎","zx8974sczzez21dd")
MyAMFDM.addEventListener(ResultEvent.RESULT, myResultHandler);
MyAMFDM.addEventListener(FaultEvent.FAULT, handleFault);
}
/*******************************************
* @amfphp返回
* ******************************************/
private function myResultHandler(event:ResultEvent):void{
var RData:Boolean=event.result as Boolean
ShowAMFPHPReturnData(RData)
}
private function ShowAMFPHPReturnData(RData:Boolean):void{
trace(RData)
}
private function handleFault(event:FaultEvent):void{
trace("error==="+event)
}
3.整合Flex+ColdFusion_CFC+Jrun+MySQL2005
MXML:同上,只需要改变
<Script source="AppAS/Flex_CFC_Jrun_MySQL2005.as"/>即可
Flex_CFC_Jrun_MySQL2005.as
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import net.diding.managers.AMFDataManager;
private function DDinit():void{
/************************************************
* Coldfusion CFC
* **********************************************/
var amfChannelUri:String="http://192.168.1.102:8300/flex2gateway/";
var AMFServer:String="ColdFusion"
var MyAMFDM:AMFDataManager=AMFDataManager.getDataManager(AMFServer,"DidingCFC.helloworld",amfChannelUri)
MyAMFDM.makeRemoteCall("sayHello")
MyAMFDM.addEventListener(ResultEvent.RESULT,myResultHandler);
MyAMFDM.addEventListener(FaultEvent.FAULT, handleFault);
}
private function myResultHandler(event:ResultEvent):void{
/*******************************************
* @CFC返回
* ******************************************/
var rs:Object=event.result as Object
ShowCFCReturnData(rs)
}
private function ShowCFCReturnData(rs:Object):void{
for(var i:int=0; i<rs.length; i++){
trace(rs[i].id)
trace(rs[i].userName);
trace(rs[i].userPWD)
}
}
private function handleFault(event:FaultEvent):void{
trace("error==="+event)
}
4.整合Flex+LCDS+Jrun+MSSQL2005
MXML:同上,只需要改变
<Script source="AppAS/Flex_LCDS_Jrun_MSSQL2005.as"/>即可
Flex_LCDS_Jrun_MSSQL2005.as
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import net.diding.managers.AMFDataManager;
private function DDinit():void{
/************************************************
* LCDS不支持指定源,所以第二个参数设为null,Jrun配置
* **********************************************/
var amfChannelUri:String="http://192.168.1.102:8600/blazeds/messagebroker/amf"
var AMFServer:String="TestJavaMySQL"
var MyAMFDM:AMFDataManager=AMFDataManager.getDataManager(AMFServer,"net.diding.ReadFlexJavaData",amfChannelUri)
MyAMFDM.makeRemoteCall("getNotices")
MyAMFDM.addEventListener(ResultEvent.RESULT, myResultHandler);
MyAMFDM.addEventListener(FaultEvent.FAULT, handleFault);
}
/*******************************************
* @LCDS返回
* ******************************************/
private function myResultHandler(event:ResultEvent):void{
var rs:Object=event.result as Object
ShowAMFLCDSReturnData(rs)
}
private function ShowAMFLCDSReturnData(RData:Object):void{
for(var j:int=0;j<RData.length;j++){
trace(RData[j].userName)
}
}
private function handleFault(event:FaultEvent):void{
trace("error==="+event)
}
三.AS RemoteAMF3类源码:
AMFDataManager.as
package net.diding.managers
{
import flash.events.EventDispatcher;
import mx.managers.CursorManager;
import mx.messaging.Channel;
import mx.messaging.ChannelSet;
import mx.messaging.channels.AMFChannel;
import mx.rpc.AbstractOperation;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.mxml.RemoteObject;
/*************************************************************
* --AMFDataManager--
* @四川资中二中 帝鼎2008.06
* 单例类,先创建类,然后用创建的类调用getDataManager();
* 目前支持:AMFPHP1.9,CFC,FluorineFx,LCDS
* ***********************************************************/
public class AMFDataManager extends EventDispatcher
{
private var CFsvc:RemoteObject;
private var CFSource:String;
private static var instanceMap:Object=new Object();
private var amfChannelSet:ChannelSet
public function AMFDataManager(pri:PrivateTempClass, dest:String, CFSource:String, amfChannelURI:String)
{
this.CFsvc=new RemoteObject();
CFsvc.destination=dest;
CFsvc.source=CFSource;
CFsvc.showBusyCursor=true;
setUpAmfChannel(amfChannelURI)
}
public function setUpAmfChannel(amfChannelURI:String):void
{
var amfChannelID:String=JudgeAMFSvc(amfChannelURI)
var amfChannel:Channel=new AMFChannel(amfChannelID, amfChannelURI)
amfChannelSet=new ChannelSet();
amfChannelSet.addChannel(amfChannel);
this.CFsvc.channelSet=amfChannelSet;
}
private function JudgeAMFSvc(gAMFSvc:String):String
{
var returnSvcKind:String;
var num:Number=gAMFSvc.lastIndexOf("/");
var theString:String=gAMFSvc.substring(num + 1);
//cfc
if (theString.length == 0)
{
returnSvcKind="my-cfamf"
}
else
{
//amfphp
if (theString == "gateway.php")
{
returnSvcKind="my-amfphp";
}
else
//fluorine
if (theString == "Gateway.aspx")
{
returnSvcKind="my-amf"
}
else
//java
if (theString == "amf")
{
returnSvcKind="my-amf"
}
}
return returnSvcKind;
}
public static function getDataManager(CFsvc:String, Source:String, amfChannelURI:String):AMFDataManager
{
var CFSource:String=Source;
if (AMFDataManager.instanceMap[CFSource] == null)
{
AMFDataManager.instanceMap[CFSource]=new AMFDataManager(new PrivateTempClass(), CFsvc, CFSource, amfChannelURI);
}
var MyAMFDM_ER:AMFDataManager=AMFDataManager.instanceMap[CFSource];
return MyAMFDM_ER;
}
public function makeRemoteCall(methodName:String, ... args:Array):void
{
var op:mx.rpc.AbstractOperation=CFsvc[methodName];
op.makeObjectsBindable=true;
CFsvc.addEventListener(ResultEvent.RESULT, doResults);
CFsvc.addEventListener(FaultEvent.FAULT, doFault);
if (args.length > 0)
{
op.send.apply(null, args);
}
else
{
op.send();
}
}
private function doResults(result:ResultEvent):void
{
CursorManager.removeBusyCursor();
dispatchEvent(new AMFResultEvent(ResultEvent.RESULT, false, true, result.result));
}
private function doFault(fault:FaultEvent):void
{
CFsvc.showBusyCursor=false
this.dispatchEvent(fault);
}
}
}
class PrivateTempClass
{
public function PrivateTempClass()
{
}
}
AMFResultEvent.as
package net.diding.managers
{
import flash.events.Event;
import mx.messaging.messages.IMessage;
import mx.rpc.AsyncToken;
import mx.rpc.events.ResultEvent;
public class AMFResultEvent extends ResultEvent
{
public function AMFResultEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=true, result:Object=null, token:AsyncToken=null, message:IMessage=null)
{
super(type, bubbles, cancelable, result, token, message);
}
override public function clone():Event
{
return new AMFResultEvent(type, bubbles, cancelable, result, token, message);
}
}
}