两个完全独立的swf进行数据交互【原创】

  第一步,创建一个web项目分别叫localConnection_test1,主应用的代码如下:

View Code
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
 3                xmlns:s="library://ns.adobe.com/flex/spark"  width="100%" height="100%"
 4                xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="creationCompleteHandler(event)">
 5     <fx:Script>
 6         <![CDATA[
 7             import mx.events.FlexEvent;
 8             
 9             private var conn:LocalConnection;
10             
11             protected function creationCompleteHandler(event:FlexEvent):void
12             {
13                 hi.text = "这是test1";
14                 conn = new LocalConnection();
15                 conn.addEventListener(StatusEvent.STATUS,onStatus);     
16                 conn.connect("fromtest2");     
17                 conn.client = this;     
18             }
19             
20             protected function onStatus(event:StatusEvent):void
21             {
22                 hi.text=event.level;  
23             }
24             
25             public function showText(s:String):void   
26             {  
27                 hi.text = s;   
28             }  
29             
30             protected function b1_clickHandler(event:MouseEvent):void
31             {
32                 conn.send("fromtest1","gogo");    
33             }
34             
35         ]]>
36     </fx:Script>
37     <s:VGroup verticalAlign="middle">
38         <s:Label id="hi"/>
39         <s:Button id="b1" label="test1" click="b1_clickHandler(event)"/>
40     </s:VGroup>
41 </s:Application>

  第二步,将localConnection_test1导出为bin-release

  第三步,再创建一个web项目叫localConnection_test2,主应用代码如下(注意要将第二步中导出的bin-release拷在该项目的src目录下):

View Code
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
 3                xmlns:s="library://ns.adobe.com/flex/spark" width="100%" height="100%"
 4                xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="creationCompleteHandler(event)">
 5     <fx:Script>
 6         <![CDATA[
 7             import mx.controls.Alert;
 8             import mx.events.FlexEvent;
 9             
10             private var conn:LocalConnection;
11             
12             protected function creationCompleteHandler(event:FlexEvent):void
13             {
14                 ddd.text="这是test2";     
15                 conn = new LocalConnection();     
16                 conn.client = this; 
17                 conn.connect("fromtest1");     
18                 conn.addEventListener(StatusEvent.STATUS,onStatus); 
19             }
20             
21             protected function onStatus(event:StatusEvent):void
22             {
23                 ddd.text = event.level;   
24             }
25             
26             public function gogo():void     
27             {     
28                 ddd.text="test1访问";    
29             }  
30             
31             protected function b1_clickHandler(event:MouseEvent):void
32             {
33                 conn.send("fromtest2","showText","hahaha"); 
34             }
35             
36         ]]>
37     </fx:Script>
38     <s:layout>
39         <s:VerticalLayout gap="10"/>
40     </s:layout>
41     <s:SWFLoader source="bin-release/localConnection_test1.swf"/>
42     <s:HGroup verticalAlign="middle">
43         <s:Label id="ddd"/>
44         <s:Button id="b1" label="test2" click="b1_clickHandler(event)"/>
45     </s:HGroup>
46 </s:Application>

  将项目跑起来,就会发现两个项目成功的进行数据交互。

posted @ 2013-01-30 11:05  梦飞无痕  阅读(190)  评论(0编辑  收藏  举报