How to make synchronous URL requests from ActionScript?

I have a big loop in actionscript that sends a lot of data to a URL:

for(var i=0;i<1000;i++) {
    var request:URLRequest = new URLRequest();
    request.url = url;
    request.method = URLRequestMethod.POST;
    request.data = data;

    var loader:URLLoader = new URLLoader();

    loader.load(request);
}

The problem is that URLLoader can only make asynchronous calls, it sends all these thousands of requests at once, which kill the web server.

And this is a little strange. Assume that the cycle runs for 5 minutes. There are no requests to the web server for the whole 5 minutes, then at the end they are all sent immediately. I already tried everything I could think (empty loops, callbacks, delays) - nothing helps. All requests are sent immediately no matter what.

How to make requests synchronous, so it will send one request after another? Can anyone suggest any solution?

+3
1

, , .

, , - ?

// small example to see how do the chaining call

class A extends EventDispatcher {
 private var urlLoader:URLLoader;
 private var urlRequest:URLRequest;
 private var sendCount:int=0;

 //......

 public function init(url:String):void{
  urlLoader=new URLLoader();
  urlLoader.addEventListener(Event.COMPLETE, sendData);
  urlRequest = new URLRequest();
  request.url = url;
  request.method = URLRequestMethod.POST;
  count=1000;
 }

 //....
 private var data:Object;

 //.....
 // 
 function sendData(e:Event=null):void{
  if (count-- > 0) {
   urlRequest.data = data; // put the data based on the counter
   urlLoader.load(urlRequest);
  } else {
   urlLoader.removeEventListener(Event.COMPLETE, sendData);
   dispatchEvent(new Event(Event.COMPLETE));
  }
 }
}


var a:A=new A();
a.addEventListener(Event.COMPLETE, function():void{
 trace("send finished");
}); // listen to the event complete so
    // you know when you send is finished

a.init("http://...."); // ok init your send
a.sendData(); // and start the send that will be chain each time the webserver answer
+6

Source: https://habr.com/ru/post/1764576/


All Articles