Flash Media Server Developer Documentation

Managing connections sample application

This example shows how to manage connections from both the application client and the server-side code.

Write the client code

In the client code, you need to check for specific connection codes and handle them. Create live streams or play recorded streams only when the client receives NetConnection.Connect.Success. When the client receives NetConnection.Connect.AppShutDown, all streams from server to client or client to server are shut down. In that case, close the connection to the server.

Note: See the SimpleConnectManage sample, SimpleConnectManage.as, written in ActionScript 3.0.

  1. Create a NetConnection object and call the connect() method to connect to the server.
  2. Write a netStatus event handler. In it, check for specific connection codes, and take an action for each:
    public function netStatusHandler(event:NetStatusEvent):void
    {
        trace("connected is: " + nc.connected );
        trace("event.info.level: " + event.info.level);
        trace("event.info.code: " + event.info.code);
        switch (event.info.code)
        {
            case "NetConnection.Connect.Success":
                trace("Congratulations! you're connected");
                // create live streams
                // play recorded streams
                break;
            case "NetConnection.Connect.Rejected":
                trace ("Oops! the connection was rejected");
                // try to connect again
                break;
            case "NetConnection.Connect.Failed":
                trace("The server may be down or unreachable");
                // display a message for the user
                break;
            case "NetConnection.Connect.AppShutDown":
                trace("The application is shutting down");
                // this method disconnects all stream objects
                nc.close();
                break;
            case "NetConnection.Connect.Closed":
                trace("The connection was closed successfully - goodbye");
                // display a reconnect button
                break;
            }
        }
    
    

Run the code

Note: These instructions apply to any ActionScript 3.0 example without a Flash user interface in this guide. The ActionScript 3.0 examples are provided for your convenience.

  1. Check the client-side code to see which application it connects to:
    nc.connect("rtmp://localhost/HelloServer");
    
    
  2. Register the application on the server by creating an application instance directory for it in the applications directory, for example:

    RootInstall/applications/HelloServer

  3. (Optional) Or, to use an application you already have registered with the server, change the URI used in the call to connect():
    nc.connect("rtmp://localhost/MyApplication");
    
    
  4. In Adobe Flex Builder or Eclipse with the Flex Builder plug-in, create an ActionScript project named SimpleConnectManage (choose File > New > ActionScript Project, and follow the wizard).
  5. Add the SimpleConnectManage sample files to the project.
  6. Choose Run > Debug. In the Debug window, enter SimpleConnectManage for Project and SimpleConnectManage.as for Application file. Click Debug.
  7. Close the empty application window that opens, and return to Flex Builder or Eclipse. Check the messages in the Console window.

    If the connection is successful, you should see output like this:

    connected is: true
    event.info.level: status
    event.info.code: NetConnection.Connect.Success
    Congratulations! you're connected
    [SWF] C:\samples\SimpleConnectManage\bin\SimpleConnectManage-debug.swf - 2,377 bytes after decompression