0

我需要从我的 Responder 对象返回值。现在,我有:

private function pro():int {
    gateway.connect('http://10.0.2.2:5000/gateway');
    var id:int = 0;
    function ret_pr(result:*):int {
       return result
    }
    var responder:Responder = new Responder(ret_pr);
    gateway.call('sx.xj', responder);
    return id
}

基本上,我需要知道如何将 ret_pr 的返回值转换为 id 或从该函数返回的任何内容。响应者似乎只是吃了它。我不能在其他地方使用公共变量,因为这将一次运行多次,所以我需要本地范围。

4

2 回答 2

2

这就是我编写与 AMF 服务器的连接、调用它并存储结果值的方式。请记住,结果不会立即可用,因此您将设置响应程序以在数据从服务器返回后“响应”数据。

public function init():void
{
    connection = new NetConnection();
    connection.connect('http://10.0.2.2:5000/gateway');
    setSessionID( 1 );
}
public function setSessionID(user_id:String):void
{
    var amfResponder:Responder = new Responder(setSessionIDResult, onFault);
    connection.call("ServerService.setSessionID", amfResponder , user_id);
}

private function setSessionIDResult(result:Object):void {
    id = result.id;
    // here you'd do something to notify that the data has been downloaded. I'll usually 
    // use a custom Event class that just notifies that the data is ready,but I'd store 
    // it here in the class with the AMF call to keep all my data in one place.
}
private function onFault(fault:Object):void {
    trace("AMFPHP error: "+fault);
}

我希望这可以为您指明正确的方向。

于 2009-11-12T22:48:51.820 回答
0
private function pro():int {
    gateway.connect('http://10.0.2.2:5000/gateway');
    var id:int = 0;
    function ret_pr(result:*):int {
       return result
    }
    var responder:Responder = new Responder(ret_pr);
    gateway.call('sx.xj', responder);
    return id
}

这段代码永远不会得到你想要的。您需要使用适当的结果函数。匿名函数响应者返回值不会被周围的函数使用。在这种情况下,它将始终返回 0。您在这里处理异步调用,您的逻辑需要相应地处理它。

private function pro():void {
    gateway.connect('http://10.0.2.2:5000/gateway');
    var responder:Responder = new Responder(handleResponse);
    gateway.call('sx.xj', responder);
} 

private function handleResponse(result:*):void
{
    var event:MyCustomNotificationEvent = new MyCustomNotificationEvent( 
            MyCustomNotificationEvent.RESULTS_RECEIVED, result);
    dispatchEvent(event);
    //a listener responds to this and does work on your result
    //or maybe here you add the result to an array, or some other 
    //mechanism
}

使用匿名函数/闭包的重点不会给你某种伪同步行为。

于 2009-11-12T23:02:36.793 回答