In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Wonderfl で LocalConnection

WonderflでLocalConnection
一方通行にするところまでテスト
1対でコネクションしていくので、2画面ずつの組で勝手に別コネクション作るよ
違うブラウザで組を作っても大丈夫
Get Adobe Flash player
by noenoe 14 Dec 2015
// WonderflでLocalConnection
// 一方通行にするところまでテスト
//
// 1対でコネクションしていくので、2画面ずつの組で勝手に別コネクション作るよ
// 違うブラウザで組を作っても大丈夫
package {
    import flash.display.Sprite;
    import flash.events.AsyncErrorEvent;
    import flash.events.MouseEvent;
    import flash.events.SecurityErrorEvent;
    import flash.events.StatusEvent;
    import flash.net.LocalConnection;
    import flash.text.TextField;
    import flash.text.TextFieldType;
    
    public class easyLC extends Sprite
    {
        /** マスター起動モード定数 **/
        public static const MASTER_MODE:String = "masterMode";
        /** スレーブ起動モード定数 **/
        public static const SLAVE_MODE:String = "slaveMode";
        
        private var _initConnectionName:String = "_lcBridgeInit";
        private var _initLc:LocalConnection = new LocalConnection();
        
        private var _initReceiveConnectionName:String = "_lcBridgeInitReceive";
        private var _initReceiveLc:LocalConnection = new LocalConnection();
        
        /** 送信用セッション **/
        private var _sendConnectionName:String = "";
        private var _sendLc:LocalConnection = new LocalConnection();
        
        /** 受信用セッション **/
        private var _receiveConnectionName:String = "";
        private var _receiveLc:LocalConnection = new LocalConnection();
        
        /** 受信した関数を処理するオブジェクト **/
        private var _client:Object;
        
        private var _btnColor:Number = 0xcccccc;
        
        /** コンストラクタ **/
        public function easyLC()
        {
            _client = this;
            _initLc.allowDomain("*");
            _initLc.addEventListener(StatusEvent.STATUS, _onStatus);
            _initLc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, _asyncError);
            _initLc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, _securityError);
            
            _mmode_txt = new TextField();
            _mmode_txt.background = true;
            _mmode_txt.backgroundColor = _btnColor;
            _mmode_txt.text = "MasterMode";
            _mmode_txt.selectable = false;
            _mmode_txt.border = true;
            _mmode_txt.height = 20;
            _mmode_txt.addEventListener(MouseEvent.CLICK, _onClick_MasterMode);
            this.addChild(_mmode_txt);
            
            _smode_txt = new TextField();
            _smode_txt.background = true;
            _smode_txt.backgroundColor = _btnColor;
            _smode_txt.text = "SlaveMode";
            _smode_txt.selectable = false;
            _smode_txt.border = true;
            _smode_txt.height = 20;
            _smode_txt.addEventListener(MouseEvent.CLICK, _onClick_SlaveMode);
            _smode_txt.x = 110;
            this.addChild(_smode_txt);
            
            _message_txt = new TextField();
            _message_txt.border = true;
            _message_txt.width = 400;
            _message_txt.height = 400;
            _message_txt.y = 30;
            _message_txt.text = "Wonderfl で LocalConnection\n1) 2画面立ち上げて Slave → Master の順で選択\n2)テキストを入力してSend";
            this.addChild(_message_txt);
        }
        private var _smode_txt:TextField;
        private var _mmode_txt:TextField;
        private var _message_txt:TextField;
        private var _send_txt:TextField;
        private var _sendInput_txt:TextField;
        
        private function _initSendArea():void{
            _sendInput_txt = new TextField();
            _sendInput_txt.type = TextFieldType.INPUT;
            _sendInput_txt.border = true;
            _sendInput_txt.height = 20;
            _sendInput_txt.x = 110;
            this.addChild(_sendInput_txt);
            
            _send_txt = new TextField();
            _send_txt.background = true;
            _send_txt.backgroundColor = _btnColor;
            _send_txt.text = "Send";
            _send_txt.selectable = false;
            _send_txt.border = true;
            _send_txt.width = 60;
            _send_txt.height = 20;
            _send_txt.x = 220;
            _send_txt.addEventListener(MouseEvent.CLICK, _onSendClick);
            this.addChild(_send_txt);
        }
        
        private function _onSendClick(e:MouseEvent):void{
            this.send("addMessage", "from "+this._mode + " : " + _sendInput_txt.text);
            _sendInput_txt.text = "";
        }
        
        public function addMessage(msg:String):void{
            _message_txt.appendText(msg + "\n");
        }
        
        //LocalConnection ステータスハンドリング
        private function _onStatus(e:StatusEvent):void{
            
        }
        
        private function _asyncError(e:AsyncErrorEvent):void{
            
        }
        
        private function _securityError(e:SecurityErrorEvent):void{
            
        }
        
        //マスターモードに
        private function _onClick_MasterMode(e:MouseEvent):void{
            _mmode_txt.removeEventListener(MouseEvent.CLICK, _onClick_MasterMode);
            removeChild(_smode_txt);
            init(MASTER_MODE);
        }
        
        //スレーブモードに
        private function _onClick_SlaveMode(e:MouseEvent):void{
            this.removeChild(_mmode_txt);
            _smode_txt.removeEventListener(MouseEvent.CLICK, _onClick_SlaveMode);
            _smode_txt.x = 0;
            init(SLAVE_MODE);
        }
        
        private var _mode:String;
        private var _id:String = "";
        public function init(mode:String):void{
            _mode = mode;
            
            _initLc.allowDomain("*");
            _initLc.addEventListener(StatusEvent.STATUS, _onStatus);
            _initLc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, _asyncError);
            _initLc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, _securityError);
            
            if(_mode == MASTER_MODE){
                //初期化のシェイクハンドを行うための受信用LC
                _initReceiveLc.allowDomain("*");
                _initReceiveLc.client = this;
                _initReceiveLc.connect(_initReceiveConnectionName);
                
                
                //コネクションIDの生成    
                if(_id == ""){    
                    //タイムスタンプのみ
                    _initLocalConnection(new Date().getTime().toString());
                }else{
                    //ID情報を付加
                    _initLocalConnection(_id + "_" + new Date().getTime().toString());
                }
                
                _initLc.send(_initConnectionName, "initConnection", _connectionID);
            }else{
                //Slave側の初期化待ち
                _initLc.client = this;

                try{
                    _initLc.connect(_initConnectionName);
                }catch(e:Error){
                    trace(e);
                }
            }
        }
        
        /******************************************************************
         * 双方向のローカルコネクションを初期化
         *
         */
        private var _connectionID:String;
        private function _initLocalConnection(connectionID:String):void{
            _connectionID = connectionID;
            
            if(_mode == MASTER_MODE){
                _sendConnectionName = "_slave_" + _connectionID;
                _receiveConnectionName = "_master_"+ _connectionID;
            }else{
                _sendConnectionName = "_master_"+ _connectionID;
                _receiveConnectionName = "_slave_"+ _connectionID;
            }

            _receiveLc.allowDomain("*");
            _receiveLc.client = this;
            _receiveLc.connect(_receiveConnectionName);
        }
        
        /******************************************************************
         * MASTERのコネクションを受信して初期化.
         *
         */
        private var _initStatus:Boolean = false;
        
        public function initConnection(connectionID:String):void{
            _message_txt.text = "";
            addMessage("initConnection:" + connectionID);
            //コネクション初期化
            _initLocalConnection(connectionID);
            _initReceiveLc.send(_initReceiveConnectionName, "initResult");
            
            //Masterからの通信をClose
            _initLc.close();
            
            _initStatus = true;
            _initSendArea();
        }
        
        /******************************************************************
         * 初期化の結果を受け取る関数.
         * 
         */
        public function initResult():void{
            //Slaveからの通信をClose
            _initReceiveLc.close();
            
            _message_txt.text = "";
            addMessage("initResult from Slave SWF");
            _initStatus = true;
            _initSendArea();
        }
        
        /******************************************************************
         * 送信
         * 
         */
        public function send(method:String, param:Object = null):void{
            if(_initStatus){
                if(param == null){
                    _sendLc.send(_sendConnectionName, "lcbSend", method);
                }else{
                    _sendLc.send(_sendConnectionName, "lcbSend", method, param);
                }
            }
        }
        
        public function lcbSend(method:String, param:Object):void{
            this._client[method](param);
        }
    }
    
    
}