2011-01-13 38 views
0

我想讓一個錯誤的網絡連接調用一定數量的類不會對我的服務器施加太大的壓力,所以我沒有十幾個網絡連接器運行在我的代碼中。 我想要一個可以發送調用命令的類,類將該調用添加到隊列中,然後每隔一秒查看是否有任何內容在隊列中,如果是,則調用它。這是我迄今爲止所擁有的。AS3/Flex 3交錯遠程處理+隊列

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; 
} 
} 

現在,當我打電話

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

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

絕對沒有任何反應。任何想法爲什麼或在正確的方向點爲什麼這不起作用?

+0

究竟發生了什麼?例如,在調試模式下,程序是否連接到服務器,它只是返回的數據是空的,您的應用程序是否可以運行?至少提供那麼多的信息。 – Ryan 2011-01-13 09:28:54

回答

0

您創建了NetConnection的實例,但實際上並未啓動與服務器的連接。

換句話說,

nc.connect(gateway); 

缺失。

請參閱NetConnection documentation瞭解該類的更多信息。

+0

謝謝。這是問題所在。 – 2011-01-13 19:46:53