首页 > 代码库 > [ActionScript 3.0] 两个AIR之间的通信示例LocalConnection
[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=http://www.mamicode.com/‘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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。