码迷,mamicode.com
首页 > 其他好文 > 详细

[ActionScript 3.0] 两个AIR之间的通信示例LocalConnection

时间:2016-10-11 19:08:59      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

发送方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

标签:

原文地址:http://www.cnblogs.com/frost-yen/p/5950049.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!