Receive ByteArray in Actionscript 3 from Java Servlet -


i typing question solved problem , don't wanted toss (and encouraged http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/), , decided share problem-solution.

the problem want retrieve bytes java application server, means, via servlet load in flash game replay feature.

there questions trying solve other way problem, means, as3 server (php, java, etc): how send binary data as3 through java filesystem?, how can send bytearray (from flash) , form data php?, uploading bytearray via urlrequest , pushing bytearray post. didn't find i'm sharing (correct me if i'm wrong).

well, said in question, encouraged stackoverflow answer , here is:

the servlet doget method gives byte array:

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 method body:

 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 code receive byte array data:

var url:string = "http://localhost:8888/input"; //servlet url var request:urlrequest = new urlrequest(url); //get rid of 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 bytearray; trace(bytes); //yeah, you'll nothing! //the bytes obtained request (see servlet , //mouseinput.getinbytes method body code above) written in //the sequence 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); 

well, works! can make adjustments, not using anonymous function better reading, illustrate ok! can save memory game replay feature (for debug purpose) bytearray instead of heavy xml trying.

hope helped , critics appreciated!

cheers


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -