Managing connections

Connection status codes

After the connection between client and server is made, it can break for various reasons. The network might go down, the server might stop, or the connection might be closed from the server or the client. Any change in the connection status creates a netStatus event, which has both a code and a level property describing the change. This is one code and level combination:

Code

Level

Meaning

NetConnection.Connect.Success

status

A connection has been established successfully.

See NetStatus.info in the ActionScript 3.0 Reference for a complete list of all code and level values that can be returned in a netStatus event.

When the event is returned, you can access the connection code and level with event.info.code and event.info.level. You can also check the NetConnection.connected property (which has a value of true or false) to see if the connection still exists. If the connection can’t be made or becomes unavailable, you need to take some action from the application client.

Managing connections in server-side code

An application can have server-side code in a main.asc or applicationName.asc file that manages clients trying to connect.

The server-side code has access to Client objects, which represent individual clients on the server side, and a single application object, which enables you to manage the application instance. In the server code, you use Server-Side ActionScript and the server-side information objects (see the Server-Side ActionScript Language Reference).

In the server-side code, the application can accept or reject connections from clients, shut down the application, and perform other tasks to manage the connection. When a client connects, the application receives an application.onConnect event. Likewise, when the client disconnects, the application receives an application.onDisconnect event.

To manage the connection from the server, start with application.onConnect() and application.onDisconnect() in Server-Side ActionScript.

Example: Managing connections

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.

Note:

See the SimpleConnectManage sample, SimpleConnectManage.as file.

  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.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. 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 have registered with the server, change the URI used in the call to connect():

     nc.connect("rtmp://localhost/MyApplication");
  4. In Flash Builder, 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 Flash Builder. 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

Get the server version number

Use the ActionScript 3.0 NetStatusEvent to get the server version number. The server version number is in the event.info.data.version property. The following code connects to the vod service and outputs the server version number:

import flash.net.NetConnection; 
import flash.events.NetStatusEvent; 
import flash.events.AsyncErrorEvent; 
var nc:NetConnection = new NetConnection(); 
nc.client = this; 
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); 
nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler); 
function netStatusHandler(event:NetStatusEvent):void{ 
    trace("Adobe Media Server version: " + event.info.data.version); 
    trace(event.info.code); 
} 
function asyncErrorHandler(event:AsyncErrorEvent):void{ 
    trace(event); 
} 
// The vod applicatin expects this function to check bandwidth. 
function onBWDone(...data):Number{ 
    return 0; 
} 
nc.connect("rtmp://localhost/vod");

You can also add the following line of code inside the netStatusHandler() function in the HelloServer example application:

trace("Adobe Media Server version: " + event.info.data.version);

More like this

Get help faster and easier

New user?