LocalConnection 同一域中两个swf通讯失败
很简单的一个例子,不知道为什么总是通讯失败。
两个文件send.mxml和receive.mxml,代码分别如下:
receive.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="send_creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
private var conn:LocalConnection=new LocalConnection();
protected function send_creationCompleteHandler(event:FlexEvent):void
{
// TODO Auto-generated method stub
conn.addEventListener(StatusEvent.STATUS,onStatus);
}
protected function btn_send_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
conn.send("conn","sMethod");
}
protected function onStatus(e:StatusEvent):void
{
trace(e.level);
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:Button x="240" y="186" label="发送消息" click="btn_send_clickHandler(event)"/>
</s:Application>
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="receive_creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import flash.net.LocalConnection;
import mx.events.FlexEvent;
public function sMethod():void
{
textA.appendText("just a test!");
}
protected function receive_creationCompleteHandler(event:FlexEvent):void
{
// TODO Auto-generated method stub
var conn:LocalConnection=new LocalConnection();
conn.client=this;
try
{
//conn.allowDomain('*');
conn.connect("conn");
}
catch(er:ArgumentError)
{
trace(er.toString());
}
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:TextArea x="203" y="103" width="328" height="196" id="textA"/>
</s:Application>