| Flash Media Server学习站>>www.FMScn.com |
最近花了些时间研究了一下FMS自带的组件 ,由于客户端代码都比较老了,如果有时间的话,准备尝试把 FMS 的组件全都转成 Flex 的 :)
点此下载|
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.events.ToolTipEvent; 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.__name =(this.id==null ? "_DEFAULT_" : this.id) this.__prefix = "FCConnectionLight."+this.__name+"." this.addEventListener(FlexEvent.REMOVE , onRemove); ToolTipManager.showDelay = 0;// Display immediately. ToolTipManager.hideDelay = Infinity ToolTipManager.toolTipClass = HtmlToolTip } override protected function createChildren():void { this.icon_mc = new Icon() this.icon_mc.gotoAndStop(1) this.addChild(this.icon_mc) this.toolTip = "waiting..." } override protected function measure():void { 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; } } } |
admin#flashas.net (#为@) 联系QQ:
:40777822
浙ICP备06033001号