Skip to content

Instantly share code, notes, and snippets.

@shvyrev
Created May 20, 2014 11:22
Show Gist options
  • Save shvyrev/def569ea477b7c3eb6a1 to your computer and use it in GitHub Desktop.
Save shvyrev/def569ea477b7c3eb6a1 to your computer and use it in GitHub Desktop.
/**
* Created by s.shvyrev on 20.05.2014.
*/
package {
import flash.events.EventDispatcher;
import flash.events.NetStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.net.NetConnection;
import flash.utils.clearInterval;
import flash.utils.setInterval;
public class BWChecker extends EventDispatcher {
private var _url:String;
private var nc:NetConnection;
private var bwInterval:int;
private var counter:int;
public function BWChecker(url:String) {
_url = url;
super();
}
public function check():void {
if (nc == null)
{
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, handler_netStatus);
nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handler_error);
var ncClientObj:Object = new Object();
nc.client = ncClientObj;
ncClientObj.onBWDone = ncOnBWDone;
ncClientObj.onBWCheck = ncOnBWCheck;
nc.connect(_url, true);
}
else
{
nc.close();
nc = null;
}
}
private function handler_error(event:SecurityErrorEvent):void {
trace(event.type);
}
private function handler_netStatus(event:NetStatusEvent):void {
trace("code:", event.info.code);
}
private function ncOnBWDone(kbitDown:Number, deltaDown:Number, deltaTime:Number, latency:Number)
{
trace("onBWDone: kbitDown:"+kbitDown+" deltaDown:"+deltaDown+" deltaTime:"+deltaTime+" latency:"+latency);
// app logic based on the bandwidth detected follows here
var detected_bw:Number = kbitDown;
bwInterval = setInterval(doBWCheck, 5000);
var msgStr:String = "download speed: "+kbitDown+"Kbps latency: "+latency+"ms";
// bwList.replaceItemAt({label: msgStr}, bwList.length-1);
}
private function doBWCheck()
{
clearInterval(bwInterval);
counter = 0;
nc.call("checkBandwidth", null);
// bwList.dataProvider.addItem({label: "testing performance..."});
}
public function ncOnBWCheck(param)
{
trace("onBWCheck");
return ++counter; // Serverside, just ignore any return value, For now return the call count
}
}
}
@donotfeedaslender
Copy link

 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment