[转]LocalConnecton的性能有关问题[慎用LocalConnection]

[转]LocalConnecton的性能问题[慎用LocalConnection]
http://www.cnblogs.com/zhaozhan/archive/2010/07/24/1784179.html

LocalConnection可以实现在两个SWF文件的通信,不过存在延时问题,延时的时间跟发送的数据量有关,如果只是做普通的通信到没有什么所谓,如果是利用LocalConnection做实时的数据推送就会出现很严重的延时问题。频繁进行send操作,一方面会使内存不断上升,并且消息的接收方的速度与消息的发生速度不同步,停止发送信息之后仍然可以接收到数据。
      下面通过一个简单的例子看LocalConnection的性能问题:
       发送方SWF,定时器每500毫秒发送一次数据。
  
<?xml version="1.0" encoding="utf-8"?>
  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="Init()">
      <mx:Script>
           <![CDATA[
              import flash.net.LocalConnection;
   
              private var conn:LocalConnection;
              private var SendNo:uint=0;
              private var SendTimer:Timer;
              public function Init():void{
                 conn=new LocalConnection();
                  conn.addEventListener(StatusEvent.STATUS, onStatus);
   
                 SendTimer=new Timer(500);
                 SendTimer.addEventListener(TimerEvent.TIMER,handlerSend);
                  SendTimer.start();            }
   
             public function handlerSend(event:TimerEvent):void
              {                  SendNo++;
                 var data:Array=new Array();                
                  for(var i:uint=0;i<20000;i++)
                  {
                      data[i]="a";
                  }
                  trace(new Date()+" SendNo:",SendNo);
                  conn.send("LocalConnectionTest", "testHandler",data.join(""));
                  txtMessage.text=new Date()+",SendNo:"+SendNo+ "\n"+txtMessage.text;
               }
   
 
   
              private function onStatus(event:StatusEvent):void {
                  switch (event.level) {
                      case "status":
                          trace("send() OK.");
                          break;
                      case "error":
                          trace("send() Fault");
                         break;
                  }
            }
   
          ]]>
   
        </mx:Script>
     <mx:Canvas width="400" height="300" borderColor="#333333" borderStyle="solid" borderThickness="1">
          <mx:TextInput id="txtMessage" width="100%" height="100%"/>
      </mx:Canvas>
  </mx:Application>

      接收方SWF,只做简单的字符拼接:
  
<?xml version="1.0" encoding="utf-8"?>
  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="Init()">
      <mx:Script>
          <![CDATA[
              import flash.net.LocalConnection;
   
              private var conn:LocalConnection;
              private var receiveNo:uint=0;
              public function Init():void{
                  conn=new LocalConnection();
                  conn.client=this;
                  try{
                      conn.connect("LocalConnectionTest");
                  }
                  catch(error:ArgumentError){
                      trace("ArgumentError");
                  }
             }
   
              public function testHandler(msg:String):void{
                 receiveNo++;
                 lbInfo.text+=msg+"\n";
                 trace(msg);
                 trace(new Date()+",receiveNo:"+receiveNo);
                txtMessage.text=new Date()+",receiveNo:"+receiveNo+ "\n"+txtMessage.text;
              }
          ]]>
      </mx:Script>
      
      <mx:Label id="lbInfo" visible="false"/>
      <mx:Canvas width="400" height="300" borderColor="#333333" borderStyle="solid" borderThickness="1">
          <mx:TextInput id="txtMessage" width="100%" height="100%"/>
      </mx:Canvas>
  </mx:Application>

 



[转]LocalConnecton的性能有关问题[慎用LocalConnection]