·您当前的位置:主页 > 技术教程 > AS2与AS3技术 >

[AS3]AS3基于SDK4.0下实现P2P(rtmfp协议用法)源代码示例

时间:2013-08-07 09:04CuPlayer.com
[AS3]AS3基于SDK4.0下实现P2P(rtmfp协议用法)源代码示例

[AS3]AS3基于SDK4.0下实现P2P(rtmfp协议用法)源代码示例

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
  3.                            xmlns:s="library://ns.adobe.com/flex/spark" 
  4.                            xmlns:mx="library://ns.adobe.com/flex/mx" 
  5.                            width="1000" height="400" initialize="init()"> 
  6.         <fx:Declarations> 
  7.                 <!-- Place non-visual elements (e.g., services, value objects) here --> 
  8.         </fx:Declarations> 
  9.         <fx:Script> 
  10.                 <![CDATA[ 
  11.                         import flash.utils.getTimer;                         
  12.                         import mx.collections.XMLListCollection;                         
  13.                         import mx.core.UIComponent; 
  14.                          
  15.                         private const URL:String = "rtmfp://p2p.rtmfp.net/key/"; 
  16.                          
  17.                         /** 
  18.                          * 视频渲染组  
  19.                          */ 
  20.                         private var stream:URLStream;                                 
  21.                         private var localConn:NetConnection; 
  22.                         private var player:NetStream; 
  23.                          
  24.                         /** 
  25.                          * UI组  
  26.                          */ 
  27.                         private var video:Video = new Video(); 
  28.                         private var ui:UIComponent = new UIComponent(); 
  29.                          
  30.                         /**  
  31.                          * 我的nearID 
  32.                          */ 
  33.                         private var myID:String; 
  34.                          
  35.                         /** 
  36.                          * rtmfp连接通道  
  37.                          */ 
  38.                         private var conn:NetConnection; 
  39.                          
  40.                         /** 
  41.                          * 获取远程name-id列表(基于http) 
  42.                          */ 
  43.                         private var loader:URLLoader; 
  44.                          
  45.                         /** 
  46.                          *  rtmfp订阅监听流 (提供订阅的流)  
  47.                          */ 
  48.                         private var listener:NetStream; 
  49.                         /** 
  50.                          * 相对于listener的订阅者流  
  51.                          */ 
  52.                         private var incomer:NetStream;                         
  53.                         /** 
  54.                          *  发送方根据farID和conn生成的流(模拟了提供订阅的流)   
  55.                          */ 
  56.                         private var targeter:NetStream; 
  57.                         /** 
  58.                          * 订阅者发送的消息的流 
  59.                          */ 
  60.                         private var sender:NetStream;                         
  61.                          
  62.                          
  63.                         private function init():void 
  64.                         { 
  65.                                 myNameTxt.text = "linyang" + new Date().getTime(); 
  66.                                  
  67.                                 canvas.addElement(ui); 
  68.                                  
  69.                                 ui.addChild(video); 
  70.                                 video.width = canvas.width; 
  71.                                 video.height = canvas.height; 
  72.                                  
  73.                                 conn = new NetConnection(); 
  74.                                 conn.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus); 
  75.                                 conn.connect(URL); 
  76.                         } 
  77.                          
  78.                         private function onNetStatus(event:NetStatusEvent):void 
  79.                         { 
  80.                                 switch (event.info.code) 
  81.                                 { 
  82.                                         case "NetConnection.Connect.Success": 
  83.                                                 onNetConnectSuccess(); 
  84.                                                 break;                         
  85.                                         case "NetConnection.Connect.Failed": 
  86.                                                 onNetConnectFailed(); 
  87.                                                 break; 
  88.                                 } 
  89.                         } 
  90.                          
  91.                         private function onNetConnectSuccess():void 
  92.                         { 
  93.                                 myID = conn.nearID; 
  94.                                 myIDTxt.text = myID; 
  95.                                 log("Connected myID " + myID); 
  96.                                 loader = new URLLoader(); 
  97.                                 loader.addEventListener(IOErrorEvent.IO_ERROR,onLoadListError); 
  98.                                 loader.addEventListener(Event.COMPLETE,onLoadListComplete); 
  99.                                 var request:URLRequest = new URLRequest("http://localhost/rtmfp.php"); 
  100.                                 request.method = "post"; 
  101.                                 var vars:URLVariables = new URLVariables(); 
  102.                                 vars.name = myNameTxt.text; 
  103.                                 vars.id = myID; 
  104.                                 request.data = vars; 
  105.                                 loader.load(request); 
  106.                         } 
  107.                          
  108.                         private function onLoadListError(event:IOError):void{} 
  109.                          
  110.                         private function onLoadListComplete(event:Event):void 
  111.                         { 
  112.                                 log("IdList Loaded"); 
  113.                                 var list:XMLListCollection = new XMLListCollection(XML(event.target.data).item); 
  114.                                 idList.dataProvider = list; 
  115.                                 idList.enabled = true; 
  116.                                  
  117.                                 playBtn.enabled = true; 
  118.                                  
  119.                                 initServerNs(); 
  120.                         } 
  121.                          
  122.                         private function onNetConnectFailed():void{} 
  123.                          
  124.                         private function initServerNs():void 
  125.                         { 
  126.                                 listener = new NetStream(conn , NetStream.DIRECT_CONNECTIONS); 
  127.                                 listener.addEventListener(NetStatusEvent.NET_STATUS,onListener); 
  128.                                 listener.publish("control-" + myNameTxt.text);                                 
  129.                                  
  130.                                 var l_obj:Object = new Object(); 
  131.                                 l_obj.onPeerConnect = function(targeter:NetStream):Boolean 
  132.                                 { 
  133.                                         log("From: " + targeter.farID); 
  134.                                         incomer = new NetStream(conn , targeter.farID); 
  135.                                         incomer.addEventListener(NetStatusEvent.NET_STATUS,onIncomer); 
  136.                                         incomer.play("media"); 
  137.                                          
  138.                                         localConn = new NetConnection(); 
  139.                                         localConn.connect(null); 
  140.                                          
  141.                                         player = new NetStream(localConn); 
  142.                                         player.client = this; 
  143.                                         video.attachNetStream(player); 
  144.                                         player.play(null); 
  145.                                          
  146.                                         var incomer_obj:Object = new Object(); 
  147.                                         incomer_obj.onIM = function(arr:Object):void 
  148.                                         { 
  149.                                                 log("Come onFuckYou: "); 
  150.                                                 getAppendBytes(arr); 
  151.                                         } 
  152.                                         incomer.client = incomer_obj; 
  153.                                         return true; 
  154.                                 } 
  155.                                 listener.client = l_obj;                                 
  156.                         } 
  157.                          
  158.                         private function getAppendBytes(b:Object):void 
  159.                         { 
  160.                                 b = ByteArray(b); 
  161.                                 log("getAppendBytes.length: " + b.length); 
  162.                                 player.appendBytes(b as ByteArray); 
  163.                         } 
  164.                          
  165.                         private function onListener(event:NetStatusEvent):void 
  166.                         { 
  167.                                 log("onListener " + event.info.code); 
  168.                         } 
  169.                          
  170.                         private function onListDoubleClk(event:MouseEvent):void 
  171.                         { 
  172.                                 var targetClk:Object = idList.dataProvider.getItemAt(idList.selectedIndex); 
  173.                                 var targetName:String = targetClk.@label; 
  174.                                 var targetID:String = targetClk.@id; 
  175.                                 log("Select Id: " + targetID); 
  176.                                 targeter = new NetStream(conn , targetID); 
  177.                                 targeter.addEventListener(NetStatusEvent.NET_STATUS,onCaller); 
  178.                                 targeter.play("control-" + targetName);         
  179.                                  
  180.                                 sender = new NetStream(conn , NetStream.DIRECT_CONNECTIONS); 
  181.                                 sender.addEventListener(NetStatusEvent.NET_STATUS, onSender); 
  182.                                 sender.publish("media"); 
  183.                         } 
  184.                          
  185.                         private function onIncomer(event:NetStatusEvent):void 
  186.                         { 
  187.                                 log("onIncomer " + event.info.code); 
  188.                         } 
  189.                          
  190.                         private function onCaller(event:NetStatusEvent):void 
  191.                         { 
  192.                                 log("onCaller Back " + event.info.code);                                 
  193.                         } 
  194.                          
  195.                         private function onSender(event:NetStatusEvent):void 
  196.                         { 
  197.                                 log("onSender Back " + event.info.code);                 
  198.                                 switch(event.info.code){ 
  199.                                         case "NetStream.Play.Start": 
  200.                                                 onSend(); 
  201.                                                 break; 
  202.                                 } 
  203.                         } 
  204.                          
  205.                         private function onSend():void 
  206.                         { 
  207.                                 localConn = new NetConnection(); 
  208.                                 localConn.connect(null); 
  209.                                  
  210.                                 player = new NetStream(localConn); 
  211.                                 player.client = this; 
  212.                                 video.attachNetStream(player); 
  213.                                 player.play(null); 
  214.                                  
  215.                                 stream = new URLStream(); 
  216.                                 stream.addEventListener(ProgressEvent.PROGRESS,onProgress); 
  217.                                 stream.load(new URLRequest("http://localhost/testfilms/good.flv"));        //改为你的地址                         
  218.                         } 
  219.                          
  220.                         public function onMetaData(info:Object):void 
  221.                         {                                 
  222.                         }                 
  223.                          
  224.                         private function onProgress(event:ProgressEvent):void 
  225.                         { 
  226.                                 var b:ByteArray = new ByteArray(); 
  227.                                 stream.readBytes(b,0,stream.bytesAvailable); 
  228.                                 player.appendBytes(b); 
  229.                                  
  230.                                 sender.send("onIM",b); 
  231.                         } 
  232.                          
  233.                         private function log(value:String):void 
  234.                         { 
  235.                                 infoTxt.appendText(value + "\n"); 
  236.                         } 
  237.                 ]]> 
  238.         </fx:Script> 
  239.         <s:VGroup x="10" y="10" width="294" height="64"> 
  240.                 <s:HGroup width="287" height="27"> 
  241.                         <s:Label text="MyName:"/> 
  242.                         <s:Label id="myNameTxt" width="342" height="14" text="N/A"/> 
  243.                 </s:HGroup> 
  244.                 <s:HGroup width="287" height="27"> 
  245.                         <s:Label text="MyID:"/> 
  246.                         <s:Label id="myIDTxt" width="342" height="14" text="N/A"/> 
  247.                 </s:HGroup> 
  248.         </s:VGroup> 
  249.         <s:List id="idList" x="10" y="82" width="219" height="278" enabled="false" click="onListDoubleClk(event)" labelField="@label"></s:List> 
  250.         <s:Button x="233" y="82" label="Play Video" click="onSend()" enabled="false" id="playBtn"/> 
  251.         <s:Group id="canvas" x="237" y="111" width="222" height="249"> 
  252.         </s:Group> 
  253.         <s:TextArea id="infoTxt" x="467" y="10" width="510" height="350"/> 
  254. </s:Application> 

 

热门文章推荐

请稍候...
  • 锁定云视频大聚惠
  • 相约直播超值狂欢
保利威视云直播
保利威视云视频
微信付费直播
酷播云免费在线视频整体解决方案
酷播v4.0网页播放器