0

Grateful for your reading first.

This is my code:

private function send( pkt:ByteArray )
{
     var int count = 0;
     var request:URLRequest = new URLRequest( ... );
     var loader:URLLoader = new URLLoader( ... );

     request.contentType = URLLoaderDataFormat.BINARY;
     request.method = URLRequestMethod.POST;
     loader.addEventListener(Event.COMPLETE, loader_complete);
     loader.dataFormat = URLLoaderDataFormat.BINARY;

     request.data = pkt;
     loader.load( request );

     //to pause the execution
     while( count < 100000000 );
}

The COMPLETE event will not be dispatched before the last while loop ended. That's to say after URLLoader.load( URLRequest ), the loader will not sent data away immediately( no delay )? Cause a number of loader.load(request) commands need to be executed continuely and in order, I need to send data away without delay for each load command in order. How to solve it?

Thanks.

Zachary
  • 127
  • 1
  • 2
  • 15
  • Are you trying to upload a file in chunks? – codingbuddha May 20 '13 at 06:39
  • Well I do not understand, can you clarify your problem a bit more? You can start any number(teoreticaly) load requests at the same time and you do not need that while there. – Volkan May 20 '13 at 09:17
  • As an example, there are 4 loader.load() commands, which load 4 chunks data( name c1, c2, c3, c4 in order ) to the server. The server may receive the chunks in order c2, c3, c1, c4( multi-thread ) which is not my expectation. So I wish c1 will be sent away "immediately" after loader.load( c1 ), then c2, c3 and so on. Sorry for the poor expression. – Zachary May 20 '13 at 10:43

1 Answers1

0

If I understood correctly, you are looking for something like this:

private function loader_complete(e:Event):void{
    if(this._ba.bytesAvailable > 0){
        this.send();
    }
}

private function send():void{
       var sendBA:ByteArray = new ByteArray();
       this._ba.readBytes(sendBA, 0, Math.min(1024, this._ba.bytesAvailable));
       var request:URLRequest = new URLRequest("...");
       request.contentType = URLLoaderDataFormat.BINARY;
       request.method = URLRequestMethod.POST;
       request.data = sendBA;
       var loader:URLLoader = new URLLoader(request);
       loader.addEventListener(Event.COMPLETE, loader_complete);
       loader.dataFormat = URLLoaderDataFormat.BINARY;
}
codingbuddha
  • 707
  • 5
  • 16
  • Then when the Event.COMPLETE will be dispatched? – Zachary May 22 '13 at 01:06
  • After loader has completed it´s job :) – codingbuddha May 22 '13 at 05:50
  • That's what I concern, I need the loader to completed it's job immediately( no delay, before next loader.load() command ) – Zachary May 22 '13 at 09:03
  • Pardon me, but what do you think how this works? You cannot say "I need it to complete immediately" and suddenly all laws of physics and technology disappear. Transferring data takes time. – codingbuddha May 22 '13 at 13:41
  • It's ok to be sent just before next load() command. The problem is AS will collect several load() commands and send them at the same time. I need the load() commands to be executed in order( one load() one time ). – Zachary May 23 '13 at 01:03
  • And this will happen with the code I posted. I have tried this one explicit before I put it up here and in general I use the exact approach everytime I used a chunked upload – codingbuddha May 23 '13 at 04:46