从Javaservlet接收ActionScript3中的ByteArray
我输入了一个问题,但最终我解决了问题,不想扔掉它(受到鼓励),并决定分享我的问题解决方案 问题是,我想从Java应用程序服务器检索一些字节,这意味着,通过Servlet加载到flash游戏中,以实现重播功能从Javaservlet接收ActionScript3中的ByteArray,java,actionscript-3,servlets,request,bytearray,Java,Actionscript 3,Servlets,Request,Bytearray,我输入了一个问题,但最终我解决了问题,不想扔掉它(受到鼓励),并决定分享我的问题解决方案 问题是,我想从Java应用程序服务器检索一些字节,这意味着,通过Servlet加载到flash游戏中,以实现重播功能 有一些问题试图以另一种方式解决问题,也就是说,从as3到服务器(php、java等),以及。我没有找到类似于我正在共享的内容(如果我错了,请纠正我)。正如我在问题中所说,StackOverflow鼓励我回答,下面是: 提供字节数组的Servlet doGet方法: protected voi
有一些问题试图以另一种方式解决问题,也就是说,从as3到服务器(php、java等),以及。我没有找到类似于我正在共享的内容(如果我错了,请纠正我)。正如我在问题中所说,StackOverflow鼓励我回答,下面是: 提供字节数组的Servlet doGet方法:
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
MouseInput oneInput = getMouseInput(); //abstracted (I'm using google appengine)
byte[] inputInBytes = oneInput.getInBytes();
OutputStream o = resp.getOutputStream();
o.write(inputInBytes);
o.flush();
o.close();
}
MouseInput.getInBytes方法体:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeInt(this.type);
dos.writeDouble(this.localX);
dos.writeDouble(this.localY);
dos.writeBoolean(this.buttonDown);
return baos.toByteArray();
接收字节数组数据的My Actionscript代码:
var url:String = "http://localhost:8888/input"; //servlet url
var request:URLRequest = new URLRequest(url);
//get rid of the cache issue:
var urlVariables:URLVariables = new URLVariables();
urlVariables.nocache = new Date().getTime();
request.data = urlVariables;
request.method = URLRequestMethod.GET;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, function (evt:Event) {
var loader:URLLoader = URLLoader(evt.target);
var bytes:ByteArray = loader.data as ByteArray;
trace(bytes); //yeah, you'll get nothing!
//the bytes obtained from the request (see Servlet and
//MouseInput.getInBytes method body code above) were written in
//the sequence like is read here:
trace(bytes.readInt());
trace(bytes.readDouble());
trace(bytes.readDouble());
trace(bytes.readBoolean());
}
loader.addEventListener(IOErrorEvent.IO_ERROR, function (evt:Event) {
trace("error");
});
loader.load(request);
好吧,它起作用了!显然,你可以做一些调整,比如不使用匿名函数来更好地阅读,但是为了说明它是可以的!现在我可以用ByteArray为游戏重播功能(出于调试目的)节省一些内存,而不是像我尝试的那样使用繁重的XML
希望这对你有所帮助,任何批评都会得到赞赏
干杯看来我得等7个小时才能回答!!!!