在做一款webgame,一开始项目使用BlazeDS与后台进行通信。用下来发现BlazeDS做了太多的事情,效率不是很高,所以考虑了一下,自己实现了一个类似于BlazeDS的RPC框架
客户端flex代码
public class NetAMF extends NetConnection
{
...sendData()
...getData()
}
{
...sendData()
...getData()
}
代码
public function sendData(funName:String,funData:Object=null):void
{
..............
var myRequest:URLRequest=new URLRequest();
myRequest.method=URLRequestMethod.POST;
myRequest.data=realByte;
myRequest.url=serverURL+funName;
myRequest.contentType="application/x-amf";
var myLoader:ZTURLLoader=new ZTURLLoader();
myLoader.dataFormat=URLLoaderDataFormat.BINARY;
myLoader.addEventListener(Event.COMPLETE,getData);
...........
myLoader.sendDataInfo=funName;
myLoader.load(myRequest);
}
public function getData(e:Event):void
{
var funName:String=(e.currentTarget as ZTURLLoader).sendDataInfo as String
var bytes:ByteArray=ZTURLLoader(e.currentTarget).data as ByteArray;
var obj=bytes.readObject();
var myEvent:ServerEvent=new ServerEvent(SUCCESS,obj,ServerID)
this.dispatchEvent(myEvent);
}
{
..............
var myRequest:URLRequest=new URLRequest();
myRequest.method=URLRequestMethod.POST;
myRequest.data=realByte;
myRequest.url=serverURL+funName;
myRequest.contentType="application/x-amf";
var myLoader:ZTURLLoader=new ZTURLLoader();
myLoader.dataFormat=URLLoaderDataFormat.BINARY;
myLoader.addEventListener(Event.COMPLETE,getData);
...........
myLoader.sendDataInfo=funName;
myLoader.load(myRequest);
}
public function getData(e:Event):void
{
var funName:String=(e.currentTarget as ZTURLLoader).sendDataInfo as String
var bytes:ByteArray=ZTURLLoader(e.currentTarget).data as ByteArray;
var obj=bytes.readObject();
var myEvent:ServerEvent=new ServerEvent(SUCCESS,obj,ServerID)
this.dispatchEvent(myEvent);
}
服务端通过Servlet读取数据并写回给客户端。