Example: Hello World application



The following sections do not apply to Adobe Media Streaming Server because you cannot write server-side code for that server edition.

This sample shows simple communication from the client to the server and back again. When a user clicks a button, the client connects to the server. The client calls a server-side function that returns a string. When the server replies, the client displays the string sent from the server.

The sample files are in the rootinstall\documentation\samples\HelloWorld folder.

Create the user interface

  1. Start Flash and select Create New > Flash File (ActionScript 3.0).

  2. In the Document Class field, enter HelloWorld. If you see an ActionScript Class Warning message about a missing definition—click OK. You will add the class file in the next section.

  3. Choose Windows > Components. Click User Interface and double-click Button to add it to the Stage. On the Properties tab, enter the instance name connectBtn.

  4. Add a Label component above the button, and give it the instance name textLbl.

  5. Save the file as HelloWorld.fla.

    You can save the client files to any location.

Write the client-side script

This script provides two button actions, either connecting to or disconnecting from the server. When connecting, the script calls the server with a string (“World”), which triggers a response that displays the returned string (“Hello, World!”).

  1. Choose File > New > ActionScript File. Check that the Target box has HelloWorld.fla.

  2. Declare the package and import the required Flash classes:

     package {     
         import flash.display.MovieClip; 
         import flash.net.Responder; 
         import flash.net.NetConnection; 
        import flash.events.NetStatusEvent; 
         import flash.events.MouseEvent; 
         public class HelloWorld extends MovieClip { 
  3. Inside the HelloWorld class declaration, declare variables for the connection and the server responder:

     private var nc:NetConnection; 
     private var myResponder:Responder = new Responder(onReply);
  4. Define the class constructor. Set the label and button display values, and add an event listener to the button:

     public function HelloWorld() { 
         textLbl.text = ""; 
         connectBtn.label = "Connect"; 
         connectBtn.addEventListener(MouseEvent.CLICK, connectHandler); 
  5. Define the event listener actions, which depend on the button’s current label:

     public function connectHandler(event:MouseEvent):void { 
             if (connectBtn.label == "Connect") { 
                 nc = new NetConnection(); 
                 // Connect to the server. 
                 // Call the server's client function serverHelloMsg, in HelloWorld.asc. 
                 nc.call("serverHelloMsg", myResponder, "World"); 
                 connectBtn.label = "Disconnect";                 
             } else {             
                 // Close the connection. 
                 connectBtn.label = "Connect"; 
                 textLbl.text = ""; 
  6. Define the responder function, which sets the label’s display value:

     private function onReply(result:Object):void { 
         trace("onReply received value: " + result); 
         textLbl.text = String(result); 
  7. Save the file as HelloWorld.as to the same folder as the HelloWorld.fla file.

Write the server-side script

  1. Choose File > New > ActionScript Communications File.

  2. Define the server-side function and the connection logic:

     application.onConnect = function( client ) { 
         client.serverHelloMsg = function( helloStr ) { 
             return "Hello, " + helloStr + "!"; 
         application.acceptConnection( client ); 
  3. Save the file as HelloWorld.asc in the rootinstall/applications/HelloWorld folder. (Create the “HelloWorld” folder when you save the file.)

Compile and run the application

  1. Verify that the server is running.

  2. Select the HelloWorld.fla file tab.

  3. Choose Control > Test Movie.

  4. Click the Connect button.

    “Hello, World!” is displayed, and the button label changes to Disconnect.

  5. Click the Disconnect button.

    The output of the trace() statements is displayed in the Flash Output window.

Adobe logo

Sign in to your account