flex flashplayer 程序 和 air 程序 通过 LocalConnection 通信
flashplayer 做控制端:
<?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" creationComplete="init(event)"> <fx:Script> <![CDATA[ import mx.events.FlexEvent; private var conn:LocalConnection; protected function init(event:FlexEvent):void { conn = new LocalConnection; conn.addEventListener(StatusEvent.STATUS,statusHandler); } private function statusHandler(evt:StatusEvent):void { switch (evt.level) { case "status": trace("LocalConnection.send() succeeded"); break; case "error": trace("LocalConnection.send() failed"); break; } } protected function pressedHandler(event:MouseEvent):void { var obj:Object = {name:txt.text,age:30}; conn.send('_connectionName3','methodName',obj); } ]]> </fx:Script> <s:layout> <s:VerticalLayout/> </s:layout> <s:TextInput id="txt"/> <s:Button label="test" click="pressedHandler(event)"/> </s:Application>
air 做客户端:
<?xml version="1.0" encoding="utf-8"?> <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" close="windowedapplication1_closeHandler(event)" creationComplete="init(event)"> <fx:Script> <![CDATA[ import mx.controls.Alert; import mx.events.FlexEvent; private var conn:LocalConnection; protected function init(event:FlexEvent):void { conn = new LocalConnection; conn.client = this; conn.allowDomain("*"); try { conn.connect('_connectionName3'); conn.addEventListener(StatusEvent.STATUS,statusHandler); } catch (error:ArgumentError) { trace("Can't connect...the connection name is already being used by another SWF"); } } private function statusHandler(evt:StatusEvent):void { switch (evt.level) { case "status": trace("LocalConnection.send() succeeded"); break; case "error": trace("LocalConnection.send() failed"); break; } } public function methodName(param:Object):void { if(param) { Alert.show("姓名:" + param.name); } } protected function windowedapplication1_closeHandler(event:Event):void { if (conn) conn.close(); } ]]> </fx:Script> </s:WindowedApplication>
client 对象绑定的对象可以是动态或普通类,其中中要访问的方法一定要是public的,或者直接绑定的类似这样:
conn.client = {methodName:function(o:Object):void{
Alert.show(o.name);
}};