跟踪as3中的netconnection错误

时间:2012-04-05 20:26:29

标签: actionscript-3 flash netconnection

我在尝试连接到外部文件时正确识别何时未建立连接时遇到问题。如果我有localhost(这是config.gateway1),它工作正常,但如果我关闭服务器我只会收到一个错误:

"打开网址时出错' http://localhost/amfphp/gateway.php'"

我已尝试在连接和通话上设置试用捕获。我还设置了一个事件监听器,它应该调用onFailure函数(此时只跟踪失败)。我打电话时似乎发生了这个错误,而且它不起作用......但我似乎无法指导错误发生时会发生什么。

    var gateway1:String = config.gateway1
    var gateway2:String = config.gateway2
    var connection:NetConnection = new NetConnection ;
    connection.addEventListener(IOErrorEvent.IO_ERROR,onFailure);
    try{
        connection.connect(gateway1);
        trace("It went in to the try of the connection.connect")
        //trace(connection);
        }
    catch(error:IOError){

        trace("It didn't work",error);
    }

var responder:Responder = new Responder(onResult,onFault);//onResult and onFault can be any name
var array:Array = new Array(kioskNum,questionNum);
try 
    {connection.call("dataconnect.retrieveInfo",responder,array);
    trace("It went in to the try of the connection.call");}
catch(error:IOError){
    trace("It didn't work",error);}
//trace(gateway1);

2 个答案:

答案 0 :(得分:4)

使用FaultEvent.FAULT

可以捕获加载WSDL时出错
...
webService.addEventListener(FaultEvent.FAULT, onWsdlLoadError);
webService.loadWsdl(<wsdl>);
...
protected function onWsdlLoadError(event:FaultEvent):void {
    trace("wsdl load error: " + event.fault);
}

希望这有帮助。

答案 1 :(得分:3)

您需要为NetStatusEvent.NET_STATUS添加事件监听器并检查info

    connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);

    function netStatusHandler(event:NetStatusEvent):void {
        switch (event.info.code) {
            case "NetConnection.Connect.Failed":
                //Do something on Failed
                break;
            case "NetConnection.Connect.Closed":
                //Do something on Closed
                break;
            case "NetConnection.Call.Failed":
                //Do something on Call.Failed
                break;
        }
    }