在netconnection上使用单例模式多次调用amfphp

时间:2012-02-23 16:16:52

标签: php actionscript-3 singleton request amfphp

我试图在一个ActionScript方法上调用两次AMFPHP,结果是只有第一次调用成功。第二个不会返回错误,不会发出任何警报,更可能是它甚至没有发生,我不确定原因。

以下是一个例子:

public function editSomething():void{
    if(dgChild.selectedIndex == -1){
       Alert.show("You need to select a register in order to edit it.");
       return;
    }
    _parent1.getParentByChild(dgChild.selectedItem.id);
    _parent2.getParent2ByChild(dgChild.selectedItem.id);
}

调用此方法时,第一个父项被正常调用,但第二个父项不是。

变量_parent1和_parent2是与PHP通信并保存结果的对象:

public class parentDAO{
    //Service or Class that this objectDAO will be communicating with. 
    private const _phpController:String = "parentController/";
    private var _AMFPHP:AMFPHP = AMFPHP.getInstance();

    //ArrayCollection that will be filled with data comming from PHP/Database.        
    [Bindable] private var _dataProviderAeronave:ArrayCollection;
    //I use public getter and setter for this dataProvider

    public function getParentByChild(child_id:int):void{
          _AMFPHP.GATEWAY.call(_phpController + "getParentByChild", new Responder(resultGetParent), child_id);
    }

    private function resultGetParent(result:Array):void{
            dataProviderParent = new ArrayCollection(result); 
    }
}

现在,假设变量_parent1是上面声明的一个对象,而_parent2是另一个类的另一个对象,与此非常相似,并假设两个通信都工作得非常好,现在出现问题:当我同时调用它们时一个ActionScript方法。如果我只调用其中一个,它将正常工作,但是当我调用两个服务时,第二个实际上从未被调用过。我很确定,因为我可以通过Service Capture Proxy看到它。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

每当NetConnection类调用AMFPHP时,它会自动关闭与路径的连接。所以,基本上忽略了下一个调用,我不知道为什么它不会出现在Flash调试器中。 我的解决方案是始终检查网关是否通过NetConnection类中的“已连接”[只读]属性进行连接。如果它是假的,我强制一个新的连接和voalá,脚本工作得很好。

答案 1 :(得分:0)

任何想法如何修复这个类?我有或多或少的问题,比如你amfphp在尝试沟通时已经很忙,但我正在广泛使用这个课程。 基本上它做的是调用ChannelPicker.getInstance()。getNewRemoteObject()

package communication
{
    import flash.net.NetConnection;

    import mx.core.FlexGlobals;
    import mx.managers.BrowserManager;
    import mx.managers.IBrowserManager;
    import mx.messaging.ChannelSet;
    import mx.messaging.channels.AMFChannel;
    import mx.rpc.remoting.RemoteObject;

    import spark.components.Label;

    public class ChannelPicker
    {
        private static  var instance:ChannelPicker;
        protected var channelSet:ChannelSet= new ChannelSet();
        protected var url:String;

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

        public function ChannelPicker()
        {
            do
            {
                var browser:IBrowserManager = BrowserManager.getInstance();
                browser.init("","");
                url = browser.url;
            }while (url == null || url=="");
                        /*oracle_plajva is project name which ends up being html name of my file so to get current url i need to split it */
            url = url.split(/oracle_plajva.html/)[0] + "Amfphp/index.php";
            var chanel:AMFChannel = new AMFChannel("amfphp", url);
            this.channelSet = new ChannelSet();
            this.channelSet.addChannel(chanel);
        }

        public function getChannelSet():ChannelSet{
            return channelSet;
        }

        public function getNewRemoteObject():RemoteObject{
            var o:RemoteObject = new RemoteObject();
            o.destination = "amfphp";
            o.channelSet = channelSet;
            return o;
        }

        public function getUrl():String{
            return url;
        }
    }
}