2011年8月28日 星期日

as3 RTMFP SAMPLE

overview
adobe的rtmfp Protocol,是基於p2p的架構,使用udp方式進行資料傳輸
程序會在一開始跟adobe提供的Cirrus service(Stratus service)進行連線,
取得group的連線資訊.
連線時需要一組developer key,
可到https://www.adobe.com/cfusion/entitlement/index.cfm?e=cirrus
註冊取得。
※若是使用自架的service 可以不用申請developer key,請參考 「as3 rtmfp server 架設」
以下為sample



<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx" width="628" height="250">
  <fx:Script>
    <![CDATA[
      import flash.net.*;
      private var server_adress:String;
      private var devkey:String;
      private var netConnection:NetConnection = null;
      private var netStream:NetStream = null;
      private var netGroup:NetGroup = null;
      private var nearID:String;
      private var connected:Boolean = false;
      private var sequenceNumber:uint = 0;
      private function connect():void
      {
        server_adress=serverAddress.text;
        devkey=devkey_txt.text;
        createRtmfp();
      }
      private function createRtmfp():void
      {
        netConnection = new NetConnection();
        netConnection.addEventListener(NetStatusEvent.NET_STATUS, NetStatusHandler);
        netConnection.connect(server_adress+devkey);
      }
      
      private function NetStatusHandler(e:NetStatusEvent):void
      {
        switch(e.info.code)
        {
          case "NetConnection.Connect.Success":
            OnConnect();
            break;
          case "NetConnection.Connect.Closed":
          case "NetConnection.Connect.Failed":
          case "NetConnection.Connect.Rejected":
          case "NetConnection.Connect.AppShutdown":
          case "NetConnection.Connect.InvalidApp":
            OnDisconnect();
            break;
          case "NetStream.Connect.Success":
            break;
          case "NetStream.Connect.Rejected": 
          case "NetStream.Connect.Failed":
            DoDisconnect();
            break;
          case "NetGroup.Connect.Success":
            OnNetGroupConnect();
            break;
          case "NetGroup.Connect.Rejected":
          case "NetGroup.Connect.Failed":
            DoDisconnect();
            break;
          case "NetGroup.Posting.Notify":
            console(e.info.message.sender+":"+e.info.message.message);
            break;
          case "NetStream.MulticastStream.Reset":
          case "NetStream.Buffer.Full":
            break;
          case "NetGroup.Neighbor.Connect":
            console("NetGroup.Neighbor.Connect:"+e.info.peerID);
            break;
          case "NetGroup.Neighbor.Disconnect":
            console("NetGroup.Neighbor.Disconnect:"+e.info.peerID);
            break;
          case "NetGroup.SendTo.Notify":
            console(e.info.message.sender+":"+e.info.message.message);
            break;
          case "NetGroup.Replication.Fetch.Result":
          case "NetGroup.Replication.Request": // for share object
            break;
          case "NetGroup.LocalCoverage.Notify": //
          case "NetGroup.MulticastStream.PublishNotify": // e.info.name
          case "NetGroup.MulticastStream.UnpublishNotify": // e.info.name
          case "NetGroup.Replication.Fetch.SendNotify": // e.info.index
          case "NetGroup.Replication.Fetch.Failed": // e.info.index
          case "NetGroup.Replication.Request": // e.info.index, e.info.requestID
          default:
            break;
        }
      }
      
      
      private function OnConnect():void
      {
        var groupSpecifier:GroupSpecifier;
        groupSpecifier = new GroupSpecifier("max2009lab/groupName");
        groupSpecifier.multicastEnabled = true;
        groupSpecifier.postingEnabled = true;
        groupSpecifier.serverChannelEnabled = true;//連接server的通道是否啟用...
        groupSpecifier.ipMulticastMemberUpdatesEnabled = true;
        groupSpecifier.routingEnabled = true;

        netStream = new NetStream(netConnection, groupSpecifier.groupspecWithAuthorizations());
        netStream.addEventListener(NetStatusEvent.NET_STATUS, NetStatusHandler);
        netGroup = new NetGroup(netConnection, groupSpecifier.groupspecWithAuthorizations());
        netGroup.addEventListener(NetStatusEvent.NET_STATUS, NetStatusHandler);
        nearID=netConnection.nearID;
        console("login success nearID:"+nearID);
      }

      private function OnNetGroupConnect():void
      {
        connected = true;
        netGroup.replicationStrategy = NetGroupReplicationStrategy.LOWEST_FIRST;
      }
      
      private function DoDisconnect():void
      {
        if(netConnection)
          netConnection.close();
      }
      
      private function OnDisconnect():void
      {
        console("login out");
        netConnection = null;
        netStream = null;
        netGroup = null;
        connected = false;
      }
      
      private function sendMessage():void
      {
        if(nearID_txt.text!="")
          DoPost(input_txt.text,nearID_txt.text);
        else
          DoPost(input_txt.text);//廣播
      }
      
      private function DoPost(message:String,targetNearID:String=null):void
      {
        sequenceNumber++;
        if(targetNearID==null)
          netGroup.post({'sender':nearID,'message':message,'sequence':sequenceNumber});
        else
        {
          var receiver:String=netGroup.convertPeerIDToGroupAddress(targetNearID);
          netGroup.sendToNearest({'sender':nearID,'message':message,'sequence':sequenceNumber},receiver);
        }
      }
      
      private function console(m:String):void
      {
        content_txt.appendText(m+"\n");
      }
    ]]>
  </fx:Script>
  <mx:VBox verticalCenter="0" horizontalCenter="0" verticalAlign="middle" horizontalAlign="left">
    <s:Label fontSize="30" text="RTMFP SAMPLE"/>
    <mx:HBox verticalAlign="middle" horizontalAlign="center">    
      <s:Label text="login server:"/>
      <s:Label text="address"/>
      <s:TextInput id="serverAddress" text="rtmfp://p2p.rtmfp.net/"/>
      <s:Label text="DEVKEY"/>
      <s:TextInput id="devkey_txt" text="input ur developer key"/>
      <s:Button label="connect" click="connect()"/>
      <s:Button label="disconnect" click="DoDisconnect()"/>
    </mx:HBox>
    <s:TextArea id="content_txt" width="461"/>
    <mx:HBox>
      <s:Label text="nearID"/>
      <s:TextInput id="nearID_txt" width="100"/>
      <s:TextInput id="input_txt" width="200"/>
      <s:Button label="send" click="sendMessage()"/>
    </mx:HBox>
  </mx:VBox>
</s:Application>

沒有留言:

張貼留言