本文介绍了如何从 actionscript 发出同步 URL 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我在 actionscript 中有一个大循环,它向一个 url 发送大量数据:

I have a big loop in actionscript that sends lots of data to an 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);
}

问题是因为 URLLoader 只能进行异步调用,它一次发送所有数千个请求,这会杀死网络服务器.

The problem is because URLLoader can make only asynchronous calls, it sends all those thousands requests at once which kills webserver.

此外,它的行为也有点奇怪.假设循环运行了 5 分钟.在整整 5 分钟内,没有请求到达 Web 服务器,然后最后它们都被一次性发送.我已经尝试了我能想到的一切(空循环、回调、延迟)——没有任何帮助.无论如何,所有请求都会立即发送.

Also it acts a bit strange on top of that. Lets say the loop is running for 5 minutes. For whole 5 minutes there is no requests coming to web server, then at the end they all are sent at once. I already tried everything I could possibly think of (empty loops, callbacks, delays) - nothing helps. All requests are sent at once no matter what.

如何使请求同步,从而一个接一个地发送请求?任何人都可以请提出任何解决方案吗?

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

推荐答案

不能进行同步调用,但可以等到服务器回复后再发送另一个请求.

You can't make synchronous call, but you can wait until the server answered back before sending another request.

但是,如果您真的必须在一个循环中向网络服务器发送一千个请求,那么也许存在设计缺陷?

But maybe there is a design flaw if really you have to send a thousand of request to a webserver in one loop ?

// 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

这篇关于如何从 actionscript 发出同步 URL 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 14:40