织梦CMS - 轻松建站从此开始!

罗索

FMS 3组件ConnectionLight的Flex版本

落鹤生 发布于 2011-03-12 23:35 点击:次 
先转一个简单又实用的 ConnectionLight ,鼠标放上,可以看到当前延迟,上传下载速度,还不太完美,先发上来了,只在本地测试了一下,如果有bug的话请留言,我会继续完善.
TAG:

最近花了些时间研究了一下FMS自带的组件 ,由于客户端代码都比较老了,如果有时间的话,准备尝试把 FMS 的组件全都转成 Flex 的 :)
先转一个简单又实用的 ConnectionLight ,鼠标放上,可以看到当前延迟,上传下载速度,还不太完美,先发上来了,只在本地测试了一下,如果有bug的话请留言,我会继续完善.

使用方法:

1. 由于netconnection chains 在AS 3中不能执行,所以我们要修改一行服务器端代码 (这是 flex or fms or flashplayer的一个bug,详细见: http://bugs.adobe.com/jira/browse/FLEXDOCS-94)

打开 C:\Program Files\Adobe\Flash Media Server 3\scriptlib\components\component.asc

看第 98 行
this.callPrefix = this.derivedTypeName + "/" + this.name + "/";

用下边这行替换
this.callPrefix = this.derivedTypeName + "." + this.name + ".";

2. 在你的服务端 main.asc 最上边加上一行
load("components.asc");

3. 想办法编译nfms.mxml看看结果吧
下载

/Files/soft/110312/4-110312233Q5.rar

 <-- 注意,压缩包里的ConnectionLight类有些小bug没更新,请自己用下边的代码替换,等正式发布时候会一并更新

主要代码:
package
{
  import flash.display.MovieClip;
  import flash.events.TimerEvent;
  import flash.net.NetConnection;
  import flash.utils.Timer;
 
  import mx.core.UIComponent;
  import mx.events.FlexEvent;
  import mx.managers.ToolTipManager;
 

 
  public class ConnectionLight extends UIComponent
  {
   [Embed(source="connectionLight_icons.swf", symbol="connectLight_mc")]
 private var Icon:Class;
 private var icon_mc:MovieClip
 //---------------------------------------------------------
    private var __name:String ;
   private var __prefix:String ;
      
   private var _nc:NetConnection ;//reference
 
   private var _interval:int = 2 ; //default value 2 second
   private var _threshold:Number = 0.1 // default value is 0.1 second
 
   private var _checkTimer:Timer = new Timer (500) //500 ms
 
   // tooltip
 private var latency_txt:String =""
 private var bwup_txt:String = ""
 private var bwdown_txt:String = ""
 
 //---------------------------------------------------------
    public function ConnectionLight()
    {
     super();   
     this.addEventListener(FlexEvent.REMOVE , onRemove); 
 ToolTipManager.showDelay = 0;// Display immediately.
 ToolTipManager.hideDelay = Infinity
 ToolTipManager.toolTipClass = HtmlToolTip
 
    }
   

    override protected function createChildren():void
    {
     super.createChildren()
     this.__name =(this.id==null ? "_DEFAULT_" : this.id)
     this.__prefix = "FCConnectionLight."+this.__name+"."
     this.icon_mc = new Icon()
     this.icon_mc.gotoAndStop(1)
     this.addChild(this.icon_mc)
     this.toolTip = "waiting..."
    }
 override protected function measure():void
 {
 super.measure()
 this.measuredHeight = this.measuredMinHeight = this.icon_mc.height
 this.measuredWidth = this.measuredMinWidth = this.icon_mc.width;
 }
   
    private function onRemove(eo:FlexEvent):void
    {
     this.removeEventListener(FlexEvent.REMOVE , onRemove)
     this._checkTimer.stop()
     this._checkTimer.removeEventListener(TimerEvent.TIMER ,onCheckTimer);
     this._nc.call(this.__prefix+"close",null)
     this._nc.client.FCConnectionLight[this.__name]=null
   this._nc = null
 
   
    }
   
    public function connect(p_nc:NetConnection):void
    {
     this._nc = p_nc
     if(this._nc.client.chainObject.FCConnectionLight == null)this._nc.client.chainObject.FCConnectionLight = {};     
 this._nc.client.chainObject.FCConnectionLight[this.__name] = this;
 this._nc.call(this.__prefix + "connect" , null ,this._interval*1000 )
 if(this._nc.connected) this.showGreen();
 this._checkTimer.addEventListener(TimerEvent.TIMER , onCheckTimer)
 this._checkTimer.start()
    }
   
    //********************
    // component setter
    //********************
   public function set interval(p_value:int):void
   {
   this._interval = p_value ;
   }
   
    public function set threshold(p_value:int):void
    {
     this._threshold = p_value ;
    }
   
 //--------------------------------------------
 private function isGrey():Boolean
 {
   return this.icon_mc.currentFrame == 1 ;
 }
 private function isGreen():Boolean
 {
 return this.icon_mc.currentFrame == 2 ;
 }
 private function isYellow():Boolean
 {
 return this.icon_mc.currentFrame == 3 ;
 }
 private function isRed():Boolean
 {
 return this.icon_mc.currentFrame ==4 ;
 }
 //----------------------------------------------
 private function showGrey():void
    {
     this.icon_mc.gotoAndStop(1);
    }
    private function showGreen():void
    {
     this.icon_mc.gotoAndStop(2);
    }
    private function showYellow():void
    {
     this.icon_mc.gotoAndStop(3)
    }
 private function showRed():void
 {
 this.icon_mc.gotoAndStop(4)
 }

 
 private function onCheckTimer(eo:TimerEvent):void
 {
  
 
   if(!this.isGrey() && !this._nc.connected)
   {
    this.showRed();
  
   } else if(!this.isGreen() && this.isYellow())
   {
    this.showGreen()
   }
 }
 
 // be called from server
 public function update(ping:Number , up:Number , down:Number):void
 {
  
 if(ping > 1000 * this._threshold)
 {
 this.showYellow()
 }else{
 this.showGreen()
 }
 
 var latency:Object = this.formatTime( ping /1000 )
 var upval:Object = this.formatRate(up * 8000)
 var dnval:Object = this.formatRate(down * 8000)
 
 this.latency_txt = (ping<1)? "less than 1 msec" : latency.value + " " + latency.unit;
 this.bwup_txt = upval.value + " " +upval.unit ;
 this.bwdown_txt = dnval.value + " " + dnval.unit ;
 if(ToolTipManager.currentToolTip!=null && ToolTipManager.currentTarget == this)ToolTipManager.currentToolTip.text = "Latency: <b>"+this.latency_txt+"</b><br>UP: <b>"+this.bwup_txt+"</b><br>Down: <b>"+this.bwdown_txt+"</b>"
  
 }
 
 private function formatNumber(value:Number):Object
 {
 var result:Object ;
 if ( value < 0.001 ) result = {value:0,exponent:0};
   else if ( value < 1 ) result = {value:value*1000,exponent:-3};
   else if ( value < 1000 ) result = {value:value,exponent:0};
   else if ( value < 1000000 ) result = {value:value/1000,exponent:3};
   else if ( value < 1000000000 ) result = {value:value/1000000,exponent:6};
   if ( result.value < 10 ) result.value = (Math.round(result.value*100))/100;
   else if ( result.value < 100 ) result.value = (Math.round(result.value*10))/10;
   else result.value = Math.round(result.value);
 return result;
 }
 
 private function formatTime(value:Number):Object
 {
 var fixp:Object = this.formatNumber(value);
   if ( fixp.exponent == -3 )fixp.unit = "msec";
   else if ( fixp.exponent == 0 )fixp.unit = "sec";
   return fixp;
 }
 
 private function formatRate(value:Number):Object
 {
 var fixp:Object = this.formatNumber(value);
 if ( fixp.exponent == -3 )fixp.value = 0, fixp.exponent = 0;
   else if ( fixp.exponent == 0 )fixp.unit = "bit/s";
   else if ( fixp.exponent == 3 )fixp.unit = "kbit/s";
   else if ( fixp.exponent == 6 )fixp.unit = "mbit/s";
     return fixp;
 }
  }
}

(陈新)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201103/11061.html]
本文出处:nshen.net 作者:陈新
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容