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