• [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+"
    ");
            }
            public static function getTextButton(parent:DisplayObjectContainer,text:String,x:Number,y:Number,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+"
    ");
            }
            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

  • 相关阅读:
    日志框架 log4j2 全解析
    SpringMVC开发RESTful接口
    SpringMVC 进阶
    SpringMVC 参数映射与文件上传
    SSM整合
    算法分析
    SpringMVC 入门
    数据结构与算法概念
    在普通WEB项目中使用Spring
    《算法导论》——重复元素的随机化快排Optimization For RandomizedQuickSort
  • 原文地址:https://www.cnblogs.com/frost-yen/p/5950049.html
Copyright © 2020-2023  润新知