如何从动作同步的URL请求? [英] How to make synchronous URL requests from actionscript?

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

问题描述

我有发送大量数据到一个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.

但也许是有设计缺陷,如果真的是你要送一千的请求发送到Web服务器在一个循环?

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆