title: How to reconnect a dropped connection
body: <p>I am working on re-connection of a user connection, after the user disconnects, due to slow internet connection. I need to get the user to connect back. The problem is, The connection is made successful, and also the re-connection is being done, but the same user when reconnected, it is adding as new connection at fms server. Below is the flex code for simple connection and re-connection.</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
       &lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"       applicationComplete="init();"&gt;
          &lt;mx:Script&gt;
      &lt;![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") &gt;= 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");
  
          }
      ]]&gt;
  &lt;/mx:Script&gt;
  
  
  &lt;mx:HDividedBox id="hbox" width="761" height="501"&gt;
      &lt;mx:TextArea id="log"  height="498" updateComplete="{log.verticalScrollPosition =  log.maxVerticalScrollPosition}" width="335"/&gt;
        &lt;mx:TextArea id="help" condenseWhite="true" height="495" visible="true" scroll="true" width="542"&gt;
           &lt;mx:htmlText&gt;
               &lt;![CDATA[&lt;p&gt;&lt;b&gt;Stream Reconnect&lt;/b&gt;&lt;/p&gt;
               &lt;p&gt;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.&lt;/p&gt;
               &lt;p&gt;&amp;nbsp;&lt;/p&gt;
               &lt;p&gt;&lt;b&gt;Do one of the following to disconnect and reconnect to a nework:&lt;/b&gt;
               &lt;li&gt;Disable your wireless network connection and enable it.&lt;/li&gt;
               &lt;li&gt;Unplug your ethernet cable and plug it back in.&lt;/li&gt;
               &lt;li&gt;Unplug your ethernet cable and try to connect to a wireless connection.&lt;/li&gt;&lt;/p&gt;
               &lt;p&gt;&lt;b&gt;Or, to simulate a network change, click the Disconnect button.&lt;/b&gt;&lt;/p&gt;  
               &lt;p&gt;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.)&lt;/p&gt;
               &lt;p&gt;&amp;nbsp;&lt;/p&gt;
               &lt;p&gt;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()'&lt;/p&gt;
               &lt;p&gt;&amp;nbsp;&lt;/p&gt;
               &lt;p&gt;&lt;b&gt;Smart Seek&lt;/b&gt;&lt;/p&gt;
               &lt;p&gt;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.   &lt;/p&gt;
  
               ]]&gt;
           &lt;/mx:htmlText&gt;
       &lt;/mx:TextArea&gt;        
  &lt;/mx:HDividedBox&gt;
  
  &lt;mx:Button label="send" click="ButtonClick(event)" y="504"/&gt;
  
  &lt;/mx:Application&gt;
  </code></pre>
  
  <p>FMS code (main.asc)</p>
  
  <pre><code>        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: ");
          };
  </code></pre>
  
  <p>Please let me know, where I am lacking behind. it would be very helpful, if any example is provided.</p>
  
  <p>Thanks in advance</p>