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.
The easiest way to run the sample is to install it on the same computer as the server.
RootInstall/applications/HelloServer
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.
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.
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 {
}
import flash.display.MovieClip; import flash.net.NetConnection; import flash.events.NetStatusEvent; import flash.events.MouseEvent;
public class HelloServer extends MovieClip {
private var nc:NetConnection;
}
Be sure the class extends MovieClip.
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.
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.
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.
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.