Connecting to the server

About the NetConnection class

Before a client can play or publish audio and video, it must connect to the server. The connection request is accepted or rejected by an application instance on the server. The server sends information messages back to the client. Once the application accepts the connection request, a connection is available to both the client and the server.

The NetConnection class connects a client to an application instance on the server. To connect, create an instance of the NetConnection class and call the connect() method. Pass an application instance URI to the connect() method:

 var nc:NetConnection = new NetConnection();
 nc.connect("rtmp://localhost/HelloServer");
 var nc:NetConnection = new NetConnection();  nc.connect("rtmp://localhost/HelloServer");
 var nc:NetConnection = new NetConnection(); 
 nc.connect("rtmp://localhost/HelloServer");

The server sends back a NetStatusEvent that tells the client whether it has connected successfully.

A NetConnection object is like a pipe that streams audio, video, and data from client to server, or from server to client. Once you create the NetConnection object, you can attach one or more streams to it. Streams handle the flow of audio, video, and data over a network connection.

A stream can carry more than one type of content (audio, video, and data). However, a stream flows in only one direction, from server to client or client to server.

About the application URI

The application instance URI can be absolute or relative and has the following syntax (items in brackets are optional):

 protocol:[//host][:port]/appname/[instanceName]
 protocol:[//host][:port]/appname/[instanceName]
 protocol:[//host][:port]/appname/[instanceName]

The parts of the URI are described in the following table.

Part

Example

Description

protocol:

rtmp:

The protocol used to connect to Adobe Media Server, Real-Time Messaging Protocol and its variations. For possible values, see the NetConnection.connect() entry in the ActionScript 3.0 Reference.

//host

//www.example.com

//localhost

The host name of a local or remote computer. To connect to a server on the same host computer as the client, use //localhost or omit the //host identifier.

:port

:8080

The port number to connect to on the server.

For RTMP and its variations, the default port is 1935. For HTTP and its variations, the default port is 80. To connect over the default port, do not specify the port number. Specify the port number only to connect to a port not configured as the default.

/appname/

/sudoku/

The name of a subdirectory of the rootinstall/applications folder.

You can specify another location for the “applications” directory in the fms.ini configuration file (at rootinstall/conf/fms.ini).

instanceName

room1

An instance of the application to which the client connects. For example, a chat room application can have many chat rooms: chatroom/room1, chatroom/room2, and so on.

If you do not specify an instance name, the client connects to the default application instance, named _definst_.

If the client and the application are on the same server, as they often are while developing an application, the only parts of the URI that are required are the protocol and the application name. You can also use //localhost or 127.0.0.1 for the domain name:

 rtmp:/vod/
rtmp://localhost/vod
rtmp://127.0.0.1/vod
 rtmp:/vod/ rtmp://localhost/vod rtmp://127.0.0.1/vod
 rtmp:/vod/ 
rtmp://localhost/vod 
rtmp://127.0.0.1/vod

To test these connections, open rootinstall/samples/videoPlayer/videoplayer.html in a browser. Open the Adobe Media Administration Console (rootinstall/tools/fms_adminConsole.htm) in a browser. In the sample video player, enter each URL in the Stream URL box, and click Play Stream. In the Adobe Media Administration Console, choose View Applications > Live Log to see the connection.

When the client and the application aren’t on the same server, specify a host domain name or IP address, as in any of the following:

rtmp://www.fmsserver.com/vod
rtmp://www.fmsserver.com/vod/mediaplayer1
rtmp://www.fmsserver.com/vod/mediaplayer2
rtmp://www.fmsserver.com/vod rtmp://www.fmsserver.com/vod/mediaplayer1 rtmp://www.fmsserver.com/vod/mediaplayer2
rtmp://www.fmsserver.com/vod 
rtmp://www.fmsserver.com/vod/mediaplayer1 
rtmp://www.fmsserver.com/vod/mediaplayer2

Example: Hello Server application

Note:

Adobe Media Streaming Server does not support this example.

You can find the HelloServer application in the rootinstall/documentation/samples/HelloServer folder. 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. Register the application by creating a folder in the server’s applications folder:

    rootinstall/applications/HelloServer

  2. (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");
     nc.connect("rtmp://localhost/HelloServer");
     nc.connect("rtmp://localhost/HelloServer");

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 Flash Professional, choose File > New > Flash File (ActionScript 3.0), and click OK.

  2. Choose Window > Components to open the Components panel.

  3. Double-click the Button component to add 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

  1. In Flash Professional, choose File > New > ActionScript File, and click OK.

  2. Save the ActionScript file as 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. Click OK, then OK again.

  5. 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 {
    }
     package {  }
     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 {
    }
     package samples {  }
     package samples { 
     }
  6. Within the package, import the ActionScript classes you’ll use in the script:

     import flash.display.MovieClip;
     import flash.net.NetConnection;
     import flash.events.NetStatusEvent;
     import flash.events.MouseEvent;
     import flash.display.MovieClip;  import flash.net.NetConnection;  import flash.events.NetStatusEvent;  import flash.events.MouseEvent;
     import flash.display.MovieClip; 
     import flash.net.NetConnection; 
     import flash.events.NetStatusEvent; 
     import flash.events.MouseEvent;
  7. 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;
    }
     public class HelloServer extends MovieClip {   private var nc:NetConnection;  }
     public class HelloServer extends MovieClip { 
         private var nc:NetConnection; 
     }

    Because the class extends the MovieClip class, it inherits all the properties and methods of the MovieClip class.

  8. 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);
    }
     public function HelloServer() {   // register listeners for mouse clicks on the two buttons   connectBtn.addEventListener(MouseEvent.CLICK, connectHandler);   closeBtn.addEventListener(MouseEvent.CLICK, closeHandler);  }
     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.

  9. 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");
    }
     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");  }
     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.

  10. 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();
    }
     public function closeHandler(event:MouseEvent):void {   trace("Now we're disconnecting");   nc.close();  }
     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.

  11. 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;
    }
    }
     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;   }  }
     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.

Understand the connection messages

When you run the sample and click the Connect button, you see messages like this, as long as the connection is successful:

 Okay, let's connect now
 connected is: true
 event.info.level: status
 event.info.code: NetConnection.Connect.Success
 Congratulations! you're connected
 Okay, let's connect now  connected is: true  event.info.level: status  event.info.code: NetConnection.Connect.Success  Congratulations! you're connected
 Okay, let's connect now 
 connected is: true 
 event.info.level: status 
 event.info.code: NetConnection.Connect.Success 
 Congratulations! you're connected

The line connected is: true shows the value of the NetConnection.connected property, meaning whether Flash Player is connected to the server over RTMP. The next two lines describe the netStatus event the NetConnection object sends to report its connection status:

 event.info.level: status
 event.info.code: NetConnection.Connect.Success
 event.info.level: status  event.info.code: NetConnection.Connect.Success
 event.info.level: status 
 event.info.code: NetConnection.Connect.Success

The level property can have two values, status or error. The code property describes the status of the connection. You can check for various code values in your netStatusHandler function and take action. Always check for a successful connection before you create streams or do other work in your application.

Likewise, when you click the Close button, you see the following:

 Now we're disconnecting
 connected is: false
 event.info.level: status
 event.info.code: NetConnection.Connect.Closed
 Thanks! the connection has been closed
 Now we're disconnecting  connected is: false  event.info.level: status  event.info.code: NetConnection.Connect.Closed  Thanks! the connection has been closed
 Now we're disconnecting 
 connected is: false 
 event.info.level: status 
 event.info.code: NetConnection.Connect.Closed 
 Thanks! the connection has been closed

More like this

Get help faster and easier

New user?