Flash Media Server Developer Documentation

Hello Server application

You can find the HelloServer application in the documentation/samples/HelloServer directory in the root install directory. This simple Flash application displays two buttons that enable you to connect to the server and close the connection. The output window displays messages about the connection status.

Run the application

The easiest way to run the sample is to install it on the same computer as the server.

  1. Copy the HelloServer folder from the documentation/samples directory in the Flash Media Server root install directory to a location on your client computer.
  2. Register the application by creating a folder in the server's applications folder:

    RootInstall/applications/HelloServer

  3. (Optional) To run the sample on a server installed on a different computer, open HelloServer.as and edit this line to add the URL to your server:
    nc.connect("rtmp://localhost/HelloServer");
    
    

    See "Connecting to the server" for details on how to construct a URL.

Design the Flash user interface

The sample is already built and included in the samples folder. However, these instructions show you how to recreate it, so that you can build it on your own and add to it.

  1. In Adobe Flash CS3 Professional, choose File > New > Flash File (ActionScript 3.0), and click OK.
  2. Choose Window > Components to open the Components panel.
  3. Click the Button component and drag it to the Stage.
  4. In the Properties Inspector, click the Properties tab. Select MovieClip as the instance behavior, and enter the instance name connectBtn.
  5. Click the Parameters tab, then Label. Enter Connect as the button label.
  6. Drag a second button component to the Stage.
  7. Give the second button the instance name closeBtn and the label Close.
  8. Save the FLA file, naming it HelloServer.fla.

Write the client-side code

You can find the complete ActionScript sample in HelloServer.as in the documentation/samples/HelloServer directory in the Flash Media Server root install directory. While you develop ActionScript 3.0 code, refer to the ActionScript 3.0 Language and Components Reference.

  1. In Adobe Flash CS3 Professional, choose File > New > ActionScript File, and click OK.
  2. Save the ActionScript file with a name that begins with a capital letter and has the extension .as, for example, HelloServer.as.
  3. Return to the FLA file. Choose File > Publish Settings. Click the Flash tab, then Settings.
  4. In the Document Class box, enter HelloServer. Click the green check mark to make sure the class file can be located.
  5. Click OK, then OK again.
  6. In the ActionScript file, enter a package declaration. If you saved the file to the same directory as the FLA file, do not use a package name, for example:
    package {
    }
    
    

    However, if you saved the file to a subdirectory below the FLA file, the package name must match the directory path to your ActionScript file, for example:

    package samples {
    }
    
    
  7. Within the package, import the ActionScript classes you need:
    import flash.display.MovieClip;
    import flash.net.NetConnection;
    import flash.events.NetStatusEvent;
    import flash.events.MouseEvent;
    
    
  8. After the import statements, create a class declaration. Within the class, define a variable of type NetConnection:
    public class HelloServer extends MovieClip {
        private var nc:NetConnection;
    }
    
    

    Be sure the class extends MovieClip.

  9. Write the class constructor, registering an event listener on each button:
    public function HelloServer() {
        // register listeners for mouse clicks on the two buttons
        connectBtn.addEventListener(MouseEvent.CLICK, connectHandler);
        closeBtn.addEventListener(MouseEvent.CLICK, closeHandler);
    }
    
    

    Use addEventListener() to call an event handler named connectHandler() when a click MouseEvent occurs on the Connect button. Likewise, call closeHandler() when a click MouseEvent occurs on the Close button.

  10. Write the connectHandler() function to connect to the server when a user clicks the Connect button:
    public function connectHandler(event:MouseEvent):void {
        trace("Okay, let's connect now");
        nc = new NetConnection();
        nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
        nc.connect("rtmp://localhost/HelloServer");
    }
    
    

    In connectHandler(), add an event listener to listen for a netStatus event returned by the NetConnection object. Then, connect to the application instance on the server by calling NetConnection.connect() with the correct URI. This URI connects to an application instance named HelloServer, where the server runs on the same computer as the client.

  11. Write the closeHandler() function to define what happens when a user clicks the Close button:
    public function closeHandler(event:MouseEvent):void {
        trace("Now we're disconnecting");
        nc.close();
    }
    
    

    It's a best practice to explicitly call close() to close the connection to the server.

  12. Write the netStatusHandler() function to handle netStatus objects returned by the NetConnection object:
    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" + "\n");
                break;
            case "NetConnection.Connect.Rejected":
                trace ("Oops! the connection was rejected" + "\n");
                break;
            case "NetConnection.Connect.Closed":
                trace("Thanks! the connection has been closed" + "\n");
                break;
            }
    }
    
    

A netStatus object contains an info object, which in turn contains a level and a code that describes the connection status.