Flash Media Server Developer Documentation

ActionScript 3.0 native bandwidth detection

The client should initiate bandwidth detection after successfully connecting to the server. To start bandwidth detection, call NetConnection.call(), passing it the special command checkBandwidth. No server-side code is needed.

Note: Use the Bandwidth sample, Bandwidth.as, written in ActionScript 3.0.

Edit Application.xml

 Make sure bandwidth detection is enabled in the Application.xml file for your application:

<BandwidthDetection enabled="true">

Bandwidth detection is enabled by default. You can use an Application.xml file specific to your application or one that applies to a virtual host (see Adobe Flash Media Server Configuration and Administration Guide for details).

Write the client event handler class

 Create an ActionScript 3.0 class that handles events and calls bandwidth detection on the server. It must implement the onBWCheck and onBWDone functions:

class Client {
    public function onBWCheck(... rest):Number {
        return 0;
    }
    public function onBWDone(... rest):void {
        var p_bw:Number;
        if (rest.length > 0) p_bw = rest[0];
            // your application should do something here
            // when the bandwidth check is complete
            trace("bandwidth = " + p_bw + " Kbps.");
    } 
}

The onBWCheck function must return a value, even if the value is 0. The onBWDone function should contain the application logic. This class will be a client to your main ActionScript 3.0 class.

Write the main client class

  1. Create your main ActionScript 3.0 class, giving it a package and class name of your choice:
    package {
        import flash.display.Sprite;
        import flash.net.NetConnection;
        import flash.events.NetStatusEvent;
    
        public class Bandwidth extends Sprite
        {
        }
    }
    
    

    You can create the main and client classes in the same file.

  2. In the constructor of the main class, create a NetConnection object, set the NetConnection.client property to an instance of the client class, and connect to the server:
    private var nc:NetConnection;
    
    public function Bandwidth()
    {
        nc = new NetConnection();
        nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
        nc.client = new Client();
        nc.connect("rtmp://localhost/FlashVideoApp");
    }
    
    
  3. In the netStatus event handler, call NetConnection.call() if the connection is successful, passing checkBandwidth as the command to execute and null for the response object:
    public function netStatusHandler(event:NetStatusEvent):void
    {
        switch (event.info.code)
        {
            case "NetConnection.Connect.Success":
                // calls bandwidth detection code built in to the server
                // no server-side code required
                trace("The connection was made successfully");
                nc.call("checkBandwidth", null);
                break;
            case "NetConnection.Connect.Rejected":
                trace ("sorry, the connection was rejected");
                break;
            case "NetConnection.Connect.Failed":
                trace("Failed to connect to server.");
                break;
        }
    }
    
    

    Note: The checkBandwidth() method belongs to the Client class on the server.

Run the sample

 Test the main class from Flash CS3 or Flex Builder 2. You will see output like this showing you the client's bandwidth:

[SWF] C:\samples\Bandwidth\bin\Bandwidth-debug.swf - 2,137 bytes after decompression
The connection was made successfully
bandwidth  = 7287

In this example, the Client class simply displays the bandwidth value. In your client, you should take some action, such as choosing a specific recorded video to stream to the client based on the client's bandwidth.