flash on 2011-5-24

by tepe
♥0 | Line 169 | Modified 2011-05-26 17:49:27 | MIT License
play

ActionScript3 source code

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

package {
     import flash.display.*;
    import flash.events.*;
    import flash.net.*
    import flash.text.*;
    
    public class Main extends Sprite {
        private var txt1:TextField = new TextField(); 
        private var txt2:TextField = new TextField();
        
        private var ar:Array;
        private var logar:Array;
        private var gs:GroupSpecifier;//接続グループ機能制限
        private var nc:NetConnection;
        private var group:NetGroup;
        private var reqflg:Boolean;
        private var groupName:String;
        private var cnt1:int = 1;
        private var memberInfo:Object;
        private var memberList:Array = new Array();
        
        private var NETCANVAS_CMD_1LINE:String = "1line";//通信コマンド
        private var CMD_1:String = "create";
        private var s:Sprite = new Sprite();

        public function Main() {
            
     
            txt1.text = "aaa";
            
            // グループ機能制限
            groupName = new String();
            groupName = "net.wonderfl.ozmap.test2"
            gs = new GroupSpecifier(groupName);//接続グループ名
            gs.postingEnabled = true;//ポスティング許可
            gs.ipMulticastMemberUpdatesEnabled = true;//IP情報のマルチキャスト許可
            gs.multicastEnabled = true;//NetStreamによるストリーミング許可
            gs.objectReplicationEnabled = true;//オブジェクトの複製許可
            gs.routingEnabled = true;//NetGroupでの転送メソッド許可
            gs.serverChannelEnabled = true;//メンバーへサーバーチャンネルを開く権限を付与
            
            // RTMFPコネクション
            nc = new NetConnection();
            //イベント NET_STATUS
            nc.addEventListener(NetStatusEvent.NET_STATUS, procNetStatus);
            //接続
            nc.connect("rtmfp://p2p.rtmfp.net/3ea2ccfc17227d122a776d41-31e9e4d71a17/");
            logar = new Array();
            reqflg = false;
            

        }
        
        private function initCanvas():void {
            addChild(s);
            addChild(txt1);
            addChild(txt2);
            //txt1.text = group.estimatedMemberCount.toString();
            txt1.width = 400;
            txt1.y = 400;
            txt1.textColor = 0x0000ff;
            
            txt2.width = 400;
            txt2.height = 400;
            txt2.type = "input";
            txt2.border = true;
            
            
            //txt1.text = nc.nearID.toString();
            //txt1.appendText("\n"+nc.nearNonce.toString());
            
            //stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
            //stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
            //stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
           // connectNode();

            
        }
        
        //マウスダウン 描画開始
        private function onMouseDown(e:MouseEvent):void {
            ar = new Array();
            //ar.push(e.stageX);
            //ar.push(e.stageY);
            ar.push(mouseX);
            ar.push(mouseY); 
            s.graphics.lineStyle(2,0x000000);
            //s.graphics.moveTo(e.stageX, e.stageY);
            s.graphics.moveTo(mouseX,mouseY);
        }
        //マウス移動 描画
        private function onMouseMove(e:MouseEvent):void {
            if (e.buttonDown) {
                //ar.push(e.stageX);
                //ar.push(e.stageY);
                ar.push(mouseX);
                ar.push(mouseY);
                s.graphics.lineStyle(2,0x000000);
                //s.graphics.moveTo(e.stageX, e.stageY);
                s.graphics.lineTo(mouseX,mouseY);
                
            }
        }
        //マウスアップ 描画終了
        private function onMouseUp(e:MouseEvent):void {
            
            //group.post({cmd: NETCANVAS_CMD_1LINE, msg: ar});//メッセージ送信
            group.post({cmd: CMD_1,msg:ar});
            logar.push(ar);//ログ追加
            ar = null;//クリア
            if (!reqflg) {
                group.addHaveObjects(0, 0);
            }
            //connectNode();
        }
        private function connectNode():void{
            group.post({cmd: CMD_1});
        }

        //通信イベント
        private function procNetStatus(e:NetStatusEvent):void {
            
           
            var i:int;
            var j:int;
            var now:Date = new Date();
            txt2.appendText("[ "+now.toLocaleDateString()+" ] ");
            txt2.appendText(now.toLocaleTimeString()+"    ");
            txt2.appendText(e.info.code);
            
            switch (e.info.code) {
                case "NetConnection.Connect.Success"://接続確立
                    group = new NetGroup(nc, gs.groupspecWithAuthorizations());
                    group.addEventListener(NetStatusEvent.NET_STATUS, procNetStatus);
                    txt1.text = nc.nearID +"\n"+nc.farID;
                    txt2.appendText("\n"+nc.nearID);
                    break;
                case "NetGroup.Connect.Success"://グループ接続確立
                    initCanvas();//開始
                    break;
                case "NetGroup.Neighbor.Connect"://メンバー接続  
                    if (logar.length == 0) {
                        reqflg = true;
                        group.addWantObjects(0, 0);//同期
                     }
                     cnt1++;
                     txt2.appendText(" "+cnt1.toString());
                     //txt2.appendText(" "+Math.round(group.estimatedMemberCount).toString());
                     txt2.appendText("\nPeerID:"+e.info.peerID);
                     txt2.appendText("\nネイバー:"+e.info.neighbor+"\n");
                     memberInfo[cnt1].peerID = e.info.peerID;
                     txt1.text = "> ";
                     for(var s:String in memberInfo){
                         txt1.appendText(s+" ");
                         //txt1.text = "sssssssssss";
                     }

                     
                    break;
                case "NetGroup.Neighbor.Disconnect"://メンバー切断
                    cnt1--;
                    txt2.appendText(" "+cnt1.toString());
                    //txt2.appendText(" "+Math.round(group.estimatedMemberCount).toString());
                    txt2.appendText("\nPeerID:"+e.info.peerID);
                    txt2.appendText("\nネイバー:"+e.info.neighbor+"\n");
                    
                    txt1.text = "< ";
                     for(var s2:String in memberInfo){
                         txt1.appendText(s2+" ");
                     }
                    break;
//ポスト受信------------------------
                case "NetGroup.Posting.Notify"://メンバーからのポスト受信
                    txt2.appendText(" "+e.info.message.cmd);
                    var obj:Object = e.info.message;//メッセージオブジェクト
                    var cmd:String = obj.cmd;//コマンド
                    logar.push(ar1);//メッセージをログに追記
                    group.addHaveObjects(0, 0);//同期
                    //コマンド-----------------------------------------
                    if (cmd == NETCANVAS_CMD_1LINE) {
                        var ar1:Array = obj.msg;//メッセージ
                        var shape:Shape = new Shape();
                        shape.graphics.lineStyle(2, 0x000000);
                        shape.graphics.moveTo(ar1[0], ar1[1]);//ライン開始点
                        for (j = 2; j < ar1.length; j += 2) {
                            shape.graphics.lineTo(ar1[j], ar1[j+1]);
                        }
                        addChild(shape);
                        txt1.text = cnt1.toString();
                    }
                    if(cmd == CMD_1){
                        cnt1++;
                        txt1.text = "cnt1 = "+cnt1.toString();
                    }

                    break;
                case "NetGroup.Replication.Request"://隣接ノードからの同期要求
                    txt2.appendText("\nrequestID:"+e.info.requestID+"\n");
                    if (e.info.index == 0) {
                        group.writeRequestedObject(e.info.requestID, {log: logar});
                    }
                    break;
                case "NetGroup.Replication.Fetch.Result"://隣接ノードが同期要求を満たした
                    //txt2.appendText("\n"+e.info.requestID+"\n");
                    if (e.info.index == 0) {
                        reqflg = false;
                        logar = e.info.object.log;
                        group.addHaveObjects(0, 0);
                        var shape0:Shape = new Shape();
                        shape0.graphics.lineStyle(2, 0x000000);
                        for (i = 0; i < logar.length; ++i) {
                            var ar:Array = logar[i];
                            shape0.graphics.moveTo(ar[0], ar[1]);
                            for (j = 2; j < ar.length; j += 2) {
                                shape0.graphics.lineTo(ar[j], ar[j+1]);
                            }
                        }
                        addChild(shape0);
                    }
                    break;
            }
            txt2.appendText("\n");
            txt2.scrollV = txt2.maxScrollV;
        }
    }
}