使用LocalConnction在SWF之间通信

使用LocalConnction在SWF之间通信
ReceiveTest.mxml
 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"  creationComplete ="application1_creationCompleteHandler(event)"
 4                xmlns:mx ="library://ns.adobe.com/flex/mx"  minWidth ="955"  minHeight ="600" >
 5      < s:layout >
 6          < s:BasicLayout />
 7      </ s:layout >
 8      < fx:Script >
 9          <![CDATA[
10             import mx.events.FlexEvent;
11
12             private var conn:LocalConnection;
13             private var receiveNo:uint = 0;
14             protected function application1_creationCompleteHandler(event:FlexEvent):void
15             {
16                 conn = new LocalConnection();
17                 conn.client = this;
18                 try{
19                     conn.connect("LocalConnectionTest");
20                 }catch(error:ArgumentError){
21                     trace("ArgumentError");
22                 }
23             }
24             
25             public function testHandler(msg:String):void{
26                 receiveNo ++;
27                 infoLabel.text += msg +"\n";
28                 trace(msg);
29                 trace(new Date()+",receiveNo:"+receiveNo);
30                 msgTxt.text = new Date()+",receiveNo:"+receiveNo;
31             }
32
33          ]]>
34      </ fx:Script >
35      < fx:Declarations >
36          <!--  将非可视元素(例如服务、值对象)放在此处  -->
37      </ fx:Declarations >
38      < s:Label  id ="infoLabel"  visible ="false" />
39      < mx:Canvas  width ="400"  height ="300"  borderColor ="#333333"  borderStyle ="solid" >
40          < s:TextInput  id ="msgTxt"  width ="100%"  height ="100%" />
41      </ mx:Canvas >
42 </ s:Application >
43
SendTest.mxml
 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"  creationComplete ="application1_creationCompleteHandler(event)"
 4                xmlns:mx ="library://ns.adobe.com/flex/mx"  minWidth ="955"  minHeight ="600" >
 5      < s:layout >
 6          < s:BasicLayout />
 7      </ s:layout >
 8      < fx:Script >
 9          <![CDATA[
10             import mx.events.FlexEvent;
11     
12             private var conn:LocalConnection;
13             private var sendNo:uint = 0;
14             private var sendTimer:Timer;
15             protected function application1_creationCompleteHandler(event:FlexEvent):void
16             {
17                 conn = new LocalConnection();
18                 conn.addEventListener(StatusEvent.STATUS,onStatus);
19                 sendTimer = new Timer(1000);
20                 sendTimer.addEventListener(TimerEvent.TIMER,onTimer);
21                 sendTimer.start();
22             }
23             public function onTimer(event:TimerEvent):void{
24                 sendNo++;
25                 var data:Array = new Array();
26                 for(var i:uint=0;i<200;i++){
27                     data[i]="1";
28                 }
29                 trace(new Date()+"SendNo:",sendNo);
30                 conn.send("LocalConnectionTest","testHandler",data.join(""));
31                 msgTxt.text = new Date()+",SendNo:"+sendNo+"\n"+msgTxt.text;
32             }
33
34             public function onStatus(event:StatusEvent):void{
35                 switch(event.level){
36                     case "status":
37                         trace("send() OK.");
38                         break;
39                     case "error":
40                         trace("send() Fault");
41                         break;
42                 }
43             }
44          ]]>
45      </ fx:Script >
46      < fx:Declarations >
47          <!--  将非可视元素(例如服务、值对象)放在此处  -->
48      </ fx:Declarations >
49      < mx:Canvas  width ="400"  height ="300"  borderColor ="#333333"  borderStyle ="solid" >
50          < s:TextInput  id ="msgTxt"  width ="100%"  height ="100%" />
51      </ mx:Canvas >
52 </ s:Application >
53



你可能感兴趣的:(使用LocalConnction在SWF之间通信)