16.14.检测用户带宽

问题
我想检测用户网络带宽以便优化视频回放
解决办法
通过下载一个图片,根据下载时间计算出用户的网速
讨论
遗憾的是Flash播放器并没有内建带宽检测系统,要想测出用户带宽,需要通过Flash播放器下载一个文件如JPEG文件,通过下载的大小和所花的时间可以计算出平均下载速度,根据8个比特等于1个字节,1000个字节等于1个kilobyte(KB),转换公式为:
+展开
-ActionScript
kilobits = bytes / 1000 * 8;

下面的例子代码演示如何测试带宽:
+展开
-ActionScript
package com.oreilly.as3cb.util {
import flash.events.EventDispatcher;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.util.getTimer;
public class BandwidthTest extends EventDispatcher {
private var _downloadCount:uint;
private var _bandwidthTests:Array;
private var _detectedBandwidth:Number;
private var _startTime:uint;
public function get detectedBandwidth( ):Number {
return _detectedBandwidth;
}
public function BandwidthTest( ) {
_downloadCount = 0;
_bandwidthTests = new Array( );
}
// Run the bandwidth test.
public function test( ):void {
// Use a URLLoader to load the data.
var loader:URLLoader = new URLLoader( );
// Use a URL with a unique query string to ensure the data is
// loaded from the server and not from browser cache.
var request:URLRequest = new URLRequest("bandwidthtestimage.jpg?unique="
+ (new Date( )).getTime( ));
loader.load(request);
loader.addEventListener(Event.OPEN, onStart);
loader.addEventListener(Event.COMPLETE, onLoad);
}
// When the file starts to download get the current timer value.
private function onStart(event:Event):void {
_startTime = getTimer( );
}
private function onLoad(event:Event):void {
// The download time is the timer value when the file has downloaded
// minus the timer value when the value started downloading. Then
// divide by 1000 to convert from milliseconds to seconds.
var downloadTime:Number = (getTimer( ) - _startTime) / 1000;
_downloadCount++;
// Convert from bytes to kilobits.
var kilobits:Number = event.target.bytesTotal / 1000 * 8;
// Divide the kilobits by the download time.
var kbps:Number = kilobits / downloadTime;
// Add the test value to the array.
_bandwidthTests.push(kbps);
if(_downloadCount == 1) {
// If it's only run one test then run the second.
test( );
}
else if(_downloadCount == 2) {
// If it's run two tests then determine the margin between the
// first two tests.
// If the margin is small (in this example, less than 50 kbps)
// then dispatch a complete event. If not run a test.
if(Math.abs(_bandwidthTests[0] - _bandwidthTests[1]) < 50) {
dispatchCompleteEvent( );
}
else {
test( );
}
}
else {
// Following the third test dispatch a complete event.
dispatchCompleteEvent( );
}
}
private function dispatchCompleteEvent( ):void {
// Determine the avarage bandwidth detection value.
_detectedBandwidth = 0;
var i:uint;
for(i = 0; i < _bandwidthTests.length; i++) {
_detectedBandwidth += _bandwidthTests[i];
}
_detectedBandwidth /= _downloadCount;
// Dispatch a complete event.
dispatchEvent(new Event(Event.COMPLETE));
}
}
}

可以用上面的代码进行带宽测试,代码如下:
+展开
-ActionScript
var bandwidthTester:BandwidthTest = new BandwidthTest( );
bandwidthTester.addEventListener(Event.COMPLETE, onBandwidthTest);
bandwidthTester.test( );

当complete事件触发时,访问detectedBandwidth属性得到带宽值
+展开
-ActionScript
private function onBandwidthTest(event:Event):void {
trace(event.target.detectedBandwidth);
}

加支付宝好友偷能量挖...


评论(0)网络
阅读(95)喜欢(0)flash/flex/fcs/AIR