如何重新连接断开的连接

如何重新连接断开的连接

问题描述:

由于互联网连接速度慢,我正在重新连接用户连接,用户断开连接后。我需要让用户连接回去。问题是,连接成功,并且重新连接正在完成,但同一用户重新连接时,它将作为新的连接添加到fms服务器。以下是用于简单连接和重新连接的柔性代码。如何重新连接断开的连接

<?xml version="1.0" encoding="utf-8"?> 
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"  applicationComplete="init();"> 
     <mx:Script> 
    <![CDATA[ 
     import mx.managers.PopUpManager; 
     import mx.controls.Alert; 
     private var connectionURL:String = "rtmp://localhost/Sample_connect"; 
     private var nc:NetConnection; 
     private var nsClient:Object; 
     private var video:Video; 
     private var meta:Object; 

     private var firstNetConnection:Boolean; 
     private var intervalNC:Timer; 

     public function init():void 
     { 
      debug("Flash Player Version: " + Capabilities.version); 
     // Flag that determines whether to call connectStream() or reconnectStream(). 
      firstNetConnection = new Boolean(); 
      firstNetConnection = true; 

     // A Timer to delay the reconnection. 
      intervalNC = new Timer(2000, 1); 
      intervalNC.addEventListener(TimerEvent.TIMER_COMPLETE, reconnectNetConnection); 

     // Create a NetConnection, add event listeners, and connect to the FMS application.  
      nc = new NetConnection(); 
      nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); 
      //nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); 
      nc.connect(connectionURL,"user"); 

     } 
    internal var alertt:Alert; 

    // Handles all NetStatusEvent events for the NetConnection. 
    private function netStatusHandler(event:NetStatusEvent):void { 
     trace("event.info.code: "+event.info.code); 
     debug(event.info.code); 
     switch (event.info.code) { 
      case "NetConnection.Connect.Success": 
       if (firstNetConnection) 
       { 
       // If this is the first time connecting, create and set up a NetStream. 
         connectStream(); 
       } 
       else 
       { 
       // If this isn't the first time connecting, attach the NetStream 
       // to the reconnected NetConnection. 
        reconnectStream(); 
       } 
       break; 
      case "NetConnection.Connect.Closed": 
      // After the connection closes, change the firstNetConnection flag. 
       firstNetConnection = false; 
       intervalNC.start(); 
       debug("Timer Started"); 
       alertt=Alert.show("You have been disconnected, please wait till we try to reconnect you"); 
       break; 
      case "NetConnection.Connect.Failed": 
      // If the reconnection attempt fails, try again. 
       intervalNC.start(); 
       break; 
      case "NetStream.Seek.Notify": 
      // A successful seek() call has occurred. 
      // Parse event.info.description to check whether the seek is "smart". 
       var desc:String = new String(event.info.description); 
       if(desc.indexOf("client-inBufferSeek") >= 0) 
        debug("A SMART SEEK occured"); 
       else 
        debug("A STANDARD SEEK occured"); 
       break; 
      case "NetStream.Step.Notify": 
      // A successful step() call has occurred. 
       debug("Successful NetStream.step() call"); 
       break; 
     } 
    } 
     // Called when the first NetConnection is successful. 
    private function connectStream():void { 
     debug("connectStream() called"); 
    } 

    private function debug(txt:String):void { 
     log.text += txt + "\n"; 
    } 

    // Called after a connection closes to reconnect to FMS. 
    private function reconnectNetConnection(timer:TimerEvent):void { 
     debug("reconnectNetConnection() called"); 
     //nc = new NetConnection(); 
     //nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); 
      //nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); 
     nc.connect(connectionURL,"RE-user"); 
    } 

    // Called after a successful reconnection to FMS. 
    private function reconnectStream():void { 
     debug("reconnectStream() called"); 
     PopUpManager.removePopUp(alertt); 
    // Attaches the same NetStream to the reconnected NetConnection. 
    // Flash Player knows where to start playing the stream. 

    // Add the first playlist entry. 
    // Note that to rebuild the playlist, you use APPEND_AND_WAIT. 

    } 
     private function disconnect():void 
     { 
      nc.call("disconnectClient", null); 

     } 

     public function ButtonClick(event:MouseEvent):void 
     { 
      nc.call("getdata",null,"ABCD"); 

     } 
    ]]> 
</mx:Script> 


<mx:HDividedBox id="hbox" width="761" height="501"> 
    <mx:TextArea id="log" height="498" updateComplete="{log.verticalScrollPosition = log.maxVerticalScrollPosition}" width="335"/> 
     <mx:TextArea id="help" condenseWhite="true" height="495" visible="true" scroll="true" width="542"> 
     <mx:htmlText> 
      <![CDATA[<p><b>Stream Reconnect</b></p> 
      <p>This example shows how a stream continues to play even when you lose your network connection. 
      The stream uses the buffer to continue playing while the NetConnection is reconnected.</p> 
      <p>&nbsp;</p> 
      <p><b>Do one of the following to disconnect and reconnect to a nework:</b> 
      <li>Disable your wireless network connection and enable it.</li> 
      <li>Unplug your ethernet cable and plug it back in.</li> 
      <li>Unplug your ethernet cable and try to connect to a wireless connection.</li></p> 
      <p><b>Or, to simulate a network change, click the Disconnect button.</b></p> 
      <p>The Disconnect button calls the application.disconnect() function on the server. 
      (If you call NetConnection.close() on the client, Flash Player cleans up the NetConnection 
      and NetStream objects and you can't reuse them.)</p> 
      <p>&nbsp;</p> 
      <p>The stream continues to play seamlessly. Watch the log to see the 'NetConnection.Connect.Closed' event, 
      then 'Timer Started', then either 'reconnectNetConnection() called' or 'NetConnection.Connect.Failed'. 
      If the connection attempt fails, the timer runs again and restarts the process until a successful 
      connection is made. A successful connection triggers 'NetConnection.Connect.Success' which invokes 
      'reconnectStream()'</p> 
      <p>&nbsp;</p> 
      <p><b>Smart Seek</b></p> 
      <p>Smart seek uses the back buffer and the forward buffer to seek -- the server doesn't request 
      data from the server. Click "Show Buffers" to see the buffer lengths. Click "Seek" and "Step" to move 
      the playhead. Look at the log to see whether the seek was "Smart" or "Standard". If the buffer isn't large 
      enough, Flash Player calls the server for more data, resulting in a standard seek. </p> 

      ]]> 
     </mx:htmlText> 
    </mx:TextArea>   
</mx:HDividedBox> 

<mx:Button label="send" click="ButtonClick(event)" y="504"/> 

</mx:Application> 

FMS代码(main.asc的)

 var name="connect"; 
     application.onAppStart=function() 
     { 
     trace("ON connected"); 
     } 


     application.onConnect=function(client) 
     { 
     application.acceptConnection(client); 
     trace("connected::::: "+name); 

     client.disconnectClient=function() 
     { 
     application.onDisconnect(); 
     }; 
     client.getdata=function(str) 
     { 
     name=str; 
     trace("GetData Is Called!! "+name); 
     }; 

     }; 


     application.onDisconnect=function() 
     { 
     trace("Disconnected: "); 
     }; 

请让我知道,我缺乏的背后我在哪里。如果提供任何示例,这将是非常有帮助的。

在此先感谢

你是对的,每个NetConnection到FMS服务器获取独特的FMS标识,并没有什么,你可以做的。但是,你必须要考虑,当你使用命令连接到FMS:

nc.connect(connectionURL, "user1"); 

你已经确定你的用户到服务器。唯一剩下的部分是接收服务器端脚本这样的信息:

application.onConnect = function(client, userId) 
{ 
    // userId will contain "user1" 
} 

然后你就可以收集连接的用户在服务器端脚本的信息,并确定新的连接是新用户或重联现有的用户。

干杯!

+0

Hi @Pooyan, 感谢您的回答,但问题始于您的答案结束。找到旧连接后(如果客户端已断开连接并重新连接),如何获取该用户的旧数据。 – user1647017 2013-05-09 12:51:19

+0

我不清楚问题是什么。你不能简单地将连接客户端的信息保存在服务器端脚本中吗?你指的是什么样的数据? – 2013-05-09 14:49:35

+0

它是由两个用户玩的游戏。因此,在游戏过程中,如果一个用户断开连接,然后连接回...,那么该用户的旧数据应该可供他使用。
我希望这对我的问题有所帮助。我很抱歉,如果这让你感到困惑。 – user1647017 2013-05-10 09:31:41