[ActionScript 3.0] 两个AIR之间的通信示例LocalConnection
Posted 浮生若夢♬為歡幾何
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[ActionScript 3.0] 两个AIR之间的通信示例LocalConnection相关的知识,希望对你有一定的参考价值。
发送方AIR程序:
package { import flash.display.DisplayObjectContainer; import flash.display.Sprite; import flash.events.MouseEvent; import flash.events.StatusEvent; import flash.net.LocalConnection; import flash.text.TextField; /** * @author Frost.Yen * @E-mail 871979853@qq.com * @create 2016-10-11 下午2:34:17 * */ public class Sender extends Sprite { private var _lc:LocalConnection; private var _sendBtn:TextField; private var _logTxt:TextField; private var _inputTxt:TextField; public function Sender() { initInput(); initLog(); _lc = new LocalConnection(); _lc.addEventListener(StatusEvent.STATUS, onStatus); _sendBtn = getTextButton(this,"-send->>",412,10,60,20); _sendBtn.addEventListener(MouseEvent.CLICK,onSend); } private function onSend(e:MouseEvent):void { _lc.send("app#receiver:AIR_TO_AIR","senderMethod",_inputTxt.text); } private function onStatus(event:StatusEvent):void { switch (event.level) { case "status" : log("LocalConnection.send() succeeded"); break; case "error" : log("LocalConnection.send() failed"); break; } } private function initInput():void { _inputTxt = new TextField(); _inputTxt.type = "input"; _inputTxt.border = true; _inputTxt.x = 10; _inputTxt.y = 10; _inputTxt.width = 400; _inputTxt.height = 20; _inputTxt.text = ""; this.addChild(_inputTxt); } private function initLog():void { _logTxt = new TextField(); _logTxt.width = 450; _logTxt.height = 300; _logTxt.border = true; _logTxt.x = 10; _logTxt.y = 40; this.addChild(_logTxt); } private function log(msg:String=""):void { _logTxt.appendText(msg+"\\n"); } public static function getTextButton(parent:DisplayObjectContainer,text:String,x:Number,y:Number,width:Number,height:Number):TextField { var button:TextField = new TextField(); button.autoSize = "center"; button.width = width; button.height = height; button.selectable = false; button.border = true; button.htmlText = "<a href=\'event:#\'>"+text+"</a>"; button.x = x; button.y = y; parent.addChild(button); return button; } } }
接收方AIR程序:
package { import flash.display.DisplayObjectContainer; import flash.display.Sprite; import flash.events.MouseEvent; import flash.events.StatusEvent; import flash.net.LocalConnection; import flash.text.TextField; /** * @author Frost.Yen * @E-mail 871979853@qq.com * @create 2016-10-11 下午2:34:54 * */ public class Receiver extends Sprite { private var _sendBtn:TextField; private var _lc:LocalConnection; private var _logTxt:TextField; public function Receiver() { initLog(); _lc = new LocalConnection(); _lc.client = this; _lc.allowDomain("app#Sender"); try { _lc.connect("AIR_TO_AIR"); } catch (error:ArgumentError) { log("Can\'t connect...the connection name is already being used by another SWF"); } } private function initLog():void { _logTxt = new TextField(); _logTxt.width = 450; _logTxt.height = 300; _logTxt.border = true; _logTxt.x = 10; _logTxt.y = 40; this.addChild(_logTxt); } private function log(msg:String=""):void { _logTxt.appendText(msg+"\\n"); } public function senderMethod(param:String):void { log(param); } } }
需要注意的是,发送方send函数中传入的函数名"senderMethod",在接收方程序中的必须设置为公开(public)的方法,否则会出错:
Error #2044: Unhandled AsyncErrorEvent:. text=Error #2095: flash.net.LocalConnection was unable to invoke callback senderMethod. error=ReferenceError: Error #1069: Property senderMethod not found on Receiver and there is no default value.
参考:http://www.cnblogs.com/frost-yen/p/5301779.html
还发现个有趣的是:传入的应用程序 ID可以不区分大小写,"app#receiver:AIR_TO_AIR"中的receiver
以上是关于[ActionScript 3.0] 两个AIR之间的通信示例LocalConnection的主要内容,如果未能解决你的问题,请参考以下文章
Actionscript,AS3,MXML,Flex,Flex Builder,Flash Builder,Flash,AIR,Flash Player之关系
在 ActionScript (3.0) 中干净地合并两个数组?