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

[url]http://www.cnblogs.com/zhaozhan/archive/2010/07/24/1784179.html[/url]

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>




[img]http://dl.iteye.com/upload/attachment/447957/aa9fb28e-f5c8-36ee-b380-3c6d16d1a8f2.png[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值