如果断开连接,则在客户端重新创建共享对象(Flex和FMS)

时间:2013-05-23 12:46:42

标签: flex flex3 flash-media-server

我遇到问题,在客户端(我正在使用flex)。当用户断开连接并重新连接时,我在共享对象上获得空对象引用。我是否需要重新创建(即重新初始化)共享对象。

感谢任何帮助。

问题是:当两个用户登录并在文本输入中插入一些数据并单击发送时,数据同步没有问题。但当其中一个用户断开连接并重新连接时,该用户将不会同步任何(更新的)数据。

以下是示例代码

main.asc的

        var user_array=Array();
        var obj;
        var obj3;
        var obj1;
        application.onAppStart= function()
        {
            trace("OnAppStart");
        }

        application.onConnect = function(client, user_name)
        { 

        application.acceptConnection(client);
        monitorConnection(client);

        var so1=SharedObject.get("hello");
        client.name=user_name;
        trace("OnConnect");
        obj=client;
        user_array.push(user_name);
        so1.setProperty("names",user_array);
        obj1=user_array;
        //  callthiss(obj);


        client.sendSomething = function(name)
        {

            for(var a=0; a<application.clients.length; a++)
            {
                //for(var j=0;j<user_array.length;j++)
                //{
                    if(application.clients[a].name == name)
                    {

                        application.clients[a].call("receiveSomething",null,name);

                    }

                //}
            }
        }

        client.Onclick=function(name)
        {
            var so2=SharedObject.get("so2");
            so2.setProperty("so2",name);
        }
        };


        clientPingResponder = function(client) {

        this.onResult = function(res) {
           // trace("ClientPingResponder.onResult Received: " + res);
            clearInterval(client.cleanupTaskId);
        } 

        this.onStatus = function(info) {
            //trace("ClientPingResponder.onStatus Received: " + info);
        }
        };
        monitorConnection = function(client) {

        client.isAlive = function() {
            trace("client.isAlive has been fired");
            client.cleanupTaskId = setInterval(client.dead, 2 * 1000);
            client.call("pings", new clientPingResponder(client));
        }

        client.dead = function() {
          //  trace("cleanupTaskId: " + client.cleanupTaskId);
           // trace("monitorTaskId: " + client.monitorTaskId);

            clearInterval(client.cleanupTaskId);
            clearInterval(client.monitorTaskId);

            trace("ghost connection has been detected");

            application.disconnect(client);
        }

        client.monitorTaskId = setInterval(client.isAlive, 7*1000);
        trace("detected");
        }


        application.onDisconnect=function(client)
        {

            trace("DisConnect"+client.name);
        //  trace(client.name);
        }

clien-side code(Flex)

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" >
    <mx:Script>
        <![CDATA[
            internal var nc:NetConnection;
            internal var so1:SharedObject;
            internal var so2:SharedObject;
            internal var str:Responder=new Responder(str1);
            internal function on_login():void
            {

                if(username!=null)
                {
                    nc=new NetConnection();
                    nc.client=this;
                    nc.connect("rtmp://localhost/Sample_2",username.text);
                    nc.addEventListener(NetStatusEvent.NET_STATUS, netstatusHandler);
                }

            }

            internal function netstatusHandler(e:NetStatusEvent):void
            {   
                switch(e.info.code)
                {
                    case "NetConnection.Connect.Success":
                    status_1.text="Connected";
                    trace("so1:: "+so1);
                    if(so1==null)
                    {
                        create_SO();
                    }
                    break;

                    case "NetConnection.Connect.Rejected":
                    status_1.text="Rejected";

                    break;
                    case "NetConnection.Connect.Failed":
                    status_1.text="Failed";
                    break;
                    case "NetConnection.Connect.AppShutdown":
                    status_1.text="AppShutDown";
                    break;
                    case "NetConnection.Connect.NetworkChange":
                    status_1.text="network Change";
                    break;
                    case "NetConnection.Connect.Closed":
                    status_1.text="Disconnected";
                    break;
                }

            }

        public function pings():void
        {
            trace("a ping call from the server has been received");
        }

            internal function create_SO():void
            {
                so1=SharedObject.getRemote("hello",nc.uri,false);
                so1.connect(nc);
                so1.addEventListener(SyncEvent.SYNC, on_hello);

                so2=SharedObject.getRemote("so2",nc.uri,false);
                so2.connect(nc);
                so2.addEventListener(SyncEvent.SYNC, so2Sync);
            }   

            internal var ar:Array=new Array();

            internal function on_hello(e:SyncEvent):void
            {
                    var d:*=e.target.data;
                    for(var s:* in d) 
                    {
                        trace("SYNC::: "+d[s]);
                        ar= d[s] as Array;  
                    }
            }

            internal function str1(strr:String):void
            {
                trace("str: "+strr);
            }
            public function onclient():void
            {
                trace("ss: ");
            }
            public function onclicka():void
            {
                for(var i:int=0;i<ar.length;i++)
                {
                    if(ar[i]!=username.text)
                    {
                        trace("CALL SEND SOME: "+ar[i]);
                        nc.call("sendSomething",null,ar[i]);
                    }
                }

            }

            //public function receiveSomething(str:String):String
            public function receiveSomething(stsr:String):String
            {
                trace("receiveSomething: "+stsr);
                    rec.text="receiveSomething: "+str;
                return stsr; 

            }
            public function ondisconnect():void{
                nc.close();
            }

            public function onreconnect():void
            {
                nc.connect("rtmp://localhost/Sample_2",username.text);
            }

            public function ButtonClick(event:MouseEvent):void
            {
                nc.call("Onclick",null,sendtxt.text);
            }

            public function so2Sync(event:SyncEvent):void
            {
                var output=event.target.data;
                var a:Array= new Array();
                for(var n in output)
                {
                    var obj=output[n];
                    a.push(obj);
                    dg.dataProvider=a;

                }

            }


        ]]>
    </mx:Script>

        <mx:TextInput x="10" y="10" id="username"/>
        <mx:Button x="188" y="10" label="Login" id="login" click="on_login()"/>
        <mx:Button id="Dis" label="Disconnect" click="ondisconnect()" y="40"/>
        <mx:Button id="re" label="reconnect" click="onreconnect()" y="70"/>

        <mx:Text x="379" y="12" text="Text" id="status_1"/>
        <mx:Text x="289" y="12" text="Status"/>
        <mx:TextInput x="438" y="180" id="sendtxt"/>
        <mx:Button x="632" y="180"  label="Send" click="ButtonClick(event)"/>
        <mx:List x="434" y="222" id="dg"></mx:List> 

    </mx:Application>

0 个答案:

没有答案