简体   繁体   English

AS3 / Flex 3交错远程处理+队列

[英]AS3 / Flex 3 Staggered Remoting + Queue

I am trying to make a class that staggers the net connections calls by a certain amount to not put too much pressure on my server, and so I don't have a dozen net connectors running around in my code. 我正在尝试创建一个类,该类将网络连接调用错开一定程度,以免对服务器造成太大的压力,因此我的代码中没有十几个网络连接器。 I want a class that I can send a call command to, the class adds the call to the queue, and then about every one second it see if anything is in the queue, and if so calls it. 我想要一个可以向其发送调用命令的类,该类将调用添加到队列中,然后大约每隔一秒钟查看队列中是否有任何内容,如果有,则进行调用。 This is what I have so far. 到目前为止,这就是我所拥有的。

package net
{
 import flash.events.TimerEvent;
 import flash.net.NetConnection;
 import flash.net.Responder;
 import flash.utils.Timer;


 public class Server 
 {
  private static var gateway:String = "http://localhost/gateway.php";
  private static var queue:Vector.<ServerNode>
  private static var nc:NetConnection;
  private static var instance:Server = null;
  private static var res:Responder;
  private var timer:Timer;

  public function Server(e:ServerEnforcer) {
   nc = new NetConnection();
   queue = new Vector.<ServerNode>();
   timer = new Timer(1000);
   timer.addEventListener(TimerEvent.TIMER, execCall);
   timer.start();
  }

  public static function getInstance():Server {
   if (instance == null) {
    instance = new Server(new ServerEnforcer);
   }
   return instance;
  }

  private function execCall(e:Event):void {
   if (queue.length > 0) {
    var node:ServerNode = queue.pop();
    nc.call(node.method, node.res, node.args);
   }
  }

  public function call(method:String, success:Function, failure:Function, ...args):void {
   queue.unshift(new ServerNode(method, success, failure, args));
  }

  private function serverFailure(event:Object):void {
   trace("Server Failure : " + event.description);
  }
 }
}
import flash.net.Responder;

class ServerEnforcer { } 

class ServerNode {
 public var method:String;
 public var success:Function;
 public var failure:Function;
 public var args:Array;
 public var res:Responder

 public function ServerNode(_method:String, _success:Function, _failure:Function, _args:Array) {
  method = _method;
  success = _success;
  failure = _failure;
  res = new Responder(success, failure);
  args = _args;
 }
}

Now when I call 现在当我打电话

Server.getInstance().call("Fetch.getData", parseAllData, onError)

public function parseAllData(event:Object):void {
    trace("Victory!");
}
public function onError(event:Object):void {
    trace("Error :" + event);
}

absolutely nothing happens. 绝对没有任何反应。 Any idea why or a point in the right direction why this isn't working? 知道为什么还是不正确的原因了吗?

You created an instance of the NetConnection, but haven't actually initiated a connection with the server. 您创建了NetConnection的实例,但实际上尚未启动与服务器的连接。

In other words, 换一种说法,

nc.connect(gateway);

is missing. 不见了。

See NetConnection documentation for more information on that class. 有关该类的更多信息,请参见NetConnection文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM