1
8
9 import com.sekati.core.CoreObject;
10 import com.sekati.time.StopWatch;
11 import com.sekati.utils.Delegate;
12
13
22 class com.sekati.net.Bandwidth extends CoreObject {
23
24 private var _timer:StopWatch;
25 private var _cb:Function;
26 private var _con:LoadVars;
27
28
33 public function Bandwidth(uri:String, cb:Function) {
34 super( );
35 _cb = cb;
36 _timer = new StopWatch( true );
37 _con = new LoadVars( );
38 _con.onLoad = Delegate.create( this, testLoaded );
39 _con.load( uri + "?" + Math.random( ) );
40 }
41
42 private function testLoaded(success:Boolean):Void {
43 if (success) {
44 var ms:Number = _timer.stop( );
45 var filesize:Number = _con.getBytesTotal( );
46 var speed:Number = Math.round( (filesize / 1024 * 8) / (ms / 1000) );
47 _cb( speed, filesize, ms );
48 } else {
49 throw new Error( "@@@ " + this.toString( ) + " Error: data file loading failed." );
50 }
51 }
52 }