之前自己写了REMOTING类,但看了 bico_wing (flashcom.com.cn上的名字) 的方法觉得确实比我的简单很多,所以拿来跟大家分享一下:
先建立Remoting.as文件:
/**/
/*
Remoting类
负责FLASH与数据库交互
*/
package net.smilecn.net ... {
import flash.net.Responder;
import flash.net.NetConnection;
public class Remoting extends NetConnection ...{
/**//*
构造
@param gatewayURL remoting网关地址
@param amfType 使用AMF3或AMF0
*/
public function Remoting(gatewayURL:String,amfType:uint) ...{
super();
this.objectEncoding=amfType;
this.connect(gatewayURL);
}
//##########################################################################
//
//方法
//
//##########################################################################
/**//*
* 远程返回函数
* @param remoteMethod:远程类.方法名param远程方法所需要的参数onResultFun:返回数据所调用的方法句柄.onFaultFun同理.
*
*/
public function respond(remoteMethod:String,onResultFun:*,onFaultFun:*,... param):void ...{
var parameters:Array=param;
if (param.length > 0) ...{
parameters.unshift(remoteMethod,new Responder(onResultFun,onFaultFun));
this.call.apply(this,parameters);
} else ...{
this.call(remoteMethod,new Responder(onResultFun,onFaultFun));
}
}
}
}
Remoting类
负责FLASH与数据库交互
*/
package net.smilecn.net ... {
import flash.net.Responder;
import flash.net.NetConnection;
public class Remoting extends NetConnection ...{
/**//*
构造
@param gatewayURL remoting网关地址
@param amfType 使用AMF3或AMF0
*/
public function Remoting(gatewayURL:String,amfType:uint) ...{
super();
this.objectEncoding=amfType;
this.connect(gatewayURL);
}
//##########################################################################
//
//方法
//
//##########################################################################
/**//*
* 远程返回函数
* @param remoteMethod:远程类.方法名param远程方法所需要的参数onResultFun:返回数据所调用的方法句柄.onFaultFun同理.
*
*/
public function respond(remoteMethod:String,onResultFun:*,onFaultFun:*,... param):void ...{
var parameters:Array=param;
if (param.length > 0) ...{
parameters.unshift(remoteMethod,new Responder(onResultFun,onFaultFun));
this.call.apply(this,parameters);
} else ...{
this.call(remoteMethod,new Responder(onResultFun,onFaultFun));
}
}
}
}
然后在任何地方可以调用它:
var
remote:Remoting;
remote = new Remoting( " http://localhost/remotinggame/gateway.aspx " , 0 );
remote.respond(remoting方法名,onRuslt,onFault,参数1,参数2....);
function onRuslt(re: * ): void ... {
trace(" onRuslt:"+re);
}
function onFault(fe: * ): void ... {
trace(" onFault:"+fe.code);
}
remote = new Remoting( " http://localhost/remotinggame/gateway.aspx " , 0 );
remote.respond(remoting方法名,onRuslt,onFault,参数1,参数2....);
function onRuslt(re: * ): void ... {
trace(" onRuslt:"+re);
}
function onFault(fe: * ): void ... {
trace(" onFault:"+fe.code);
}