js <-> flashテキストチャットテスト

by poepoemix forked from minimalCompsを持つクラスをロードしたらどうなるか? (diff: 57)
js <-> flashテキストチャットテスト

以前つくったこのプラグインを利用しました。
https://github.com/taktod/webSocketForRed5

flash側のクライアントはhttp://wonderfl.net/c/w6Rs
js側のクライアントはhttp://jsdo.it/poepoemix/1UI6
♥0 | Line 128 | Modified 2012-06-27 07:29:55 | MIT License | (replaced)
play

ActionScript3 source code

/**
 * Copyright poepoemix ( http://wonderfl.net/user/poepoemix )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/w6Rs
 */

// forked from poepoemix's minimalCompsを持つクラスをロードしたらどうなるか?
package
{
    import flash.events.NetStatusEvent;
    import flash.net.URLRequest;
    import flash.display.LoaderInfo;
    import flash.display.Loader;
    import flash.events.Event;
    import flash.display.Sprite;
    public class MyClass extends Sprite
    {
        private static var logger:*;
        private var helper:*;
        /**
         * コンストラクタ
         */
        public function MyClass()
        {
            if(stage) {
                pre_init();
            }
            else {
                addEventListener(Event.ADDED_TO_STAGE, pre_init);
            }
        }
        /**
         * 事前処理
         */
        private function pre_init():void {
            removeEventListener(Event.ADDED_TO_STAGE, pre_init);
            if(!stage) {
                return;
            }
            // http://wonderfl.net/c/7YEyこちらのデータをロード
            var base:MyLoader = new MyLoader("http://swf.wonderfl.net/swf/usercode/3/37/37ea/37ea71571d353e8be4841dd5730420e9bb4cfb85.swf?", function():void {
                var HelperClass:Class = base.getClass("ComponentHelper");
                helper = new HelperClass();
                helper.gray();
                logger = helper.logger();
                logger.setup(stage);
                Logger.logger = logger;
                init();
            });
        }

        /**
         * 初期化
         */
        private function init():void {
            var panel:* = helper.base({parent:this, left:10, top:10, width:stage.stageWidth - 20, height:stage.stageHeight - 20, log:true});
            var numButton:* = helper.button({parent:panel, left:340, top:10, width: panel.width - 350,text:"1人"});
            helper.panel({parent:panel, left:10, top:10,width:320,height:240});
            var commentArea:* = helper.textArea({parent:panel, left:10, top: 280, width:panel.width - 20, height:panel.height - 300, text: ""});
            commentArea.editable = false;
            var nameInput:* = helper.inputText({parent:panel, left: 11, top:260, width:50, text:"名前"});
            var commentInput:* = helper.inputText({parent:panel, left: 61, top:260, width: 320 - 52});
            helper.button({parent:panel, left: 340, top:260, width:panel.width - 350,text: "コメント", func:function():void {
                if(commentInput.text == "") {
                    return;
                }
                nc.call("comment", null, nameInput.text + ":" + commentInput.text);
                logger.info(nameInput.text + ":" + commentInput.text);
                commentInput.text = "";
            }});
            var nc:NetConnectionEx = new NetConnectionEx(
                function(data:String):void {
                    commentArea.text = data + "\n" + commentArea.text;
                },
                function(num:String):void {
                    numButton.label = num + "人";
                }
            );
            var skinSelector:* = helper.comboBox({parent:panel, left: 340, top: 30, "default":"Gray", list:["Red", "Blue", "Orange", "Green", "Aqua", "Gray"]});
            skinSelector.width = panel.width - 350;
            skinSelector.addEventListener(Event.SELECT, function(event:Event):void {
                switch(skinSelector.selectedItem) {
                    case "Red":helper.red();break;
                    case "Blue":helper.blue();break;
                    case "Orange":helper.orange();break;;
                    case "Green":helper.green();break;
                    case "Aqua":helper.aqua();break;
                    case "Gray":
                    default:helper.gray();break;
                }
                helper.refresh(stage);
            });
            nc.addEventListener(NetStatusEvent.NET_STATUS, function(event:NetStatusEvent):void {
                logger.info(event.info.code);
            });
            nc.connect("rtmp://49.212.39.17:1936/chat");
        }
    }
}
import flash.net.NetConnection;
import flash.display.LoaderInfo;
import flash.system.ApplicationDomain;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.Loader;

class Logger {
    public static var logger:*;
}

/**
 * ロード動作の補助
 */
class MyLoader {
    private var domain:ApplicationDomain = null;
    public function MyLoader(url:String, func:Function) {
        var loader:Loader = new Loader();
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(event:Event):void {
            domain = LoaderInfo(event.target).applicationDomain;
            func();
        });
        loader.load(new URLRequest(url + (new Date()).getTime()));
    }
    public function getClass(name:String):Class {
        if(domain == null) {
            throw new Error("loading is not complete yet...");
        }
        return domain.getDefinition(name) as Class;
    }
}

class NetConnectionEx extends NetConnection {
    private var msgFunc:Function = null;
    private var numFunc:Function = null;
    public function NetConnectionEx(message:Function, num:Function) {
        msgFunc = message;
        numFunc = num;
        super();
    }
    public function message(data:String):void{
        if(msgFunc != null) {
            msgFunc(data);
        }
        Logger.logger.info(data);
    }
    public function num(data:String):void {
        if(numFunc != null) {
            numFunc(data);
        }
        Logger.logger.info(data);
    }
}