[AS3]AS3基于SDK4.0下实现P2P(rtmfp协议用法)源代码示例
[AS3]AS3基于SDK4.0下实现P2P(rtmfp协议用法)源代码示例
[AS3]AS3基于SDK4.0下实现P2P(rtmfp协议用法)源代码示例
- <?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="1000" height="400" initialize="init()">
- <fx:Declarations>
- <!-- Place non-visual elements (e.g., services, value objects) here -->
- </fx:Declarations>
- <fx:Script>
- <![CDATA[
- import flash.utils.getTimer;
- import mx.collections.XMLListCollection;
- import mx.core.UIComponent;
- private const URL:String = "rtmfp://p2p.rtmfp.net/key/";
- /**
- * 视频渲染组
- */
- private var stream:URLStream;
- private var localConn:NetConnection;
- private var player:NetStream;
- /**
- * UI组
- */
- private var video:Video = new Video();
- private var ui:UIComponent = new UIComponent();
- /**
- * 我的nearID
- */
- private var myID:String;
- /**
- * rtmfp连接通道
- */
- private var conn:NetConnection;
- /**
- * 获取远程name-id列表(基于http)
- */
- private var loader:URLLoader;
- /**
- * rtmfp订阅监听流 (提供订阅的流)
- */
- private var listener:NetStream;
- /**
- * 相对于listener的订阅者流
- */
- private var incomer:NetStream;
- /**
- * 发送方根据farID和conn生成的流(模拟了提供订阅的流)
- */
- private var targeter:NetStream;
- /**
- * 订阅者发送的消息的流
- */
- private var sender:NetStream;
- private function init():void
- {
- myNameTxt.text = "linyang" + new Date().getTime();
- canvas.addElement(ui);
- ui.addChild(video);
- video.width = canvas.width;
- video.height = canvas.height;
- conn = new NetConnection();
- conn.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus);
- conn.connect(URL);
- }
- private function onNetStatus(event:NetStatusEvent):void
- {
- switch (event.info.code)
- {
- case "NetConnection.Connect.Success":
- onNetConnectSuccess();
- break;
- case "NetConnection.Connect.Failed":
- onNetConnectFailed();
- break;
- }
- }
- private function onNetConnectSuccess():void
- {
- myID = conn.nearID;
- myIDTxt.text = myID;
- log("Connected myID " + myID);
- loader = new URLLoader();
- loader.addEventListener(IOErrorEvent.IO_ERROR,onLoadListError);
- loader.addEventListener(Event.COMPLETE,onLoadListComplete);
- var request:URLRequest = new URLRequest("http://localhost/rtmfp.php");
- request.method = "post";
- var vars:URLVariables = new URLVariables();
- vars.name = myNameTxt.text;
- vars.id = myID;
- request.data = vars;
- loader.load(request);
- }
- private function onLoadListError(event:IOError):void{}
- private function onLoadListComplete(event:Event):void
- {
- log("IdList Loaded");
- var list:XMLListCollection = new XMLListCollection(XML(event.target.data).item);
- idList.dataProvider = list;
- idList.enabled = true;
- playBtn.enabled = true;
- initServerNs();
- }
- private function onNetConnectFailed():void{}
- private function initServerNs():void
- {
- listener = new NetStream(conn , NetStream.DIRECT_CONNECTIONS);
- listener.addEventListener(NetStatusEvent.NET_STATUS,onListener);
- listener.publish("control-" + myNameTxt.text);
- var l_obj:Object = new Object();
- l_obj.onPeerConnect = function(targeter:NetStream):Boolean
- {
- log("From: " + targeter.farID);
- incomer = new NetStream(conn , targeter.farID);
- incomer.addEventListener(NetStatusEvent.NET_STATUS,onIncomer);
- incomer.play("media");
- localConn = new NetConnection();
- localConn.connect(null);
- player = new NetStream(localConn);
- player.client = this;
- video.attachNetStream(player);
- player.play(null);
- var incomer_obj:Object = new Object();
- incomer_obj.onIM = function(arr:Object):void
- {
- log("Come onFuckYou: ");
- getAppendBytes(arr);
- }
- incomer.client = incomer_obj;
- return true;
- }
- listener.client = l_obj;
- }
- private function getAppendBytes(b:Object):void
- {
- b = ByteArray(b);
- log("getAppendBytes.length: " + b.length);
- player.appendBytes(b as ByteArray);
- }
- private function onListener(event:NetStatusEvent):void
- {
- log("onListener " + event.info.code);
- }
- private function onListDoubleClk(event:MouseEvent):void
- {
- var targetClk:Object = idList.dataProvider.getItemAt(idList.selectedIndex);
- var targetName:String = targetClk.@label;
- var targetID:String = targetClk.@id;
- log("Select Id: " + targetID);
- targeter = new NetStream(conn , targetID);
- targeter.addEventListener(NetStatusEvent.NET_STATUS,onCaller);
- targeter.play("control-" + targetName);
- sender = new NetStream(conn , NetStream.DIRECT_CONNECTIONS);
- sender.addEventListener(NetStatusEvent.NET_STATUS, onSender);
- sender.publish("media");
- }
- private function onIncomer(event:NetStatusEvent):void
- {
- log("onIncomer " + event.info.code);
- }
- private function onCaller(event:NetStatusEvent):void
- {
- log("onCaller Back " + event.info.code);
- }
- private function onSender(event:NetStatusEvent):void
- {
- log("onSender Back " + event.info.code);
- switch(event.info.code){
- case "NetStream.Play.Start":
- onSend();
- break;
- }
- }
- private function onSend():void
- {
- localConn = new NetConnection();
- localConn.connect(null);
- player = new NetStream(localConn);
- player.client = this;
- video.attachNetStream(player);
- player.play(null);
- stream = new URLStream();
- stream.addEventListener(ProgressEvent.PROGRESS,onProgress);
- stream.load(new URLRequest("http://localhost/testfilms/good.flv")); //改为你的地址
- }
- public function onMetaData(info:Object):void
- {
- }
- private function onProgress(event:ProgressEvent):void
- {
- var b:ByteArray = new ByteArray();
- stream.readBytes(b,0,stream.bytesAvailable);
- player.appendBytes(b);
- sender.send("onIM",b);
- }
- private function log(value:String):void
- {
- infoTxt.appendText(value + "\n");
- }
- ]]>
- </fx:Script>
- <s:VGroup x="10" y="10" width="294" height="64">
- <s:HGroup width="287" height="27">
- <s:Label text="MyName:"/>
- <s:Label id="myNameTxt" width="342" height="14" text="N/A"/>
- </s:HGroup>
- <s:HGroup width="287" height="27">
- <s:Label text="MyID:"/>
- <s:Label id="myIDTxt" width="342" height="14" text="N/A"/>
- </s:HGroup>
- </s:VGroup>
- <s:List id="idList" x="10" y="82" width="219" height="278" enabled="false" click="onListDoubleClk(event)" labelField="@label"></s:List>
- <s:Button x="233" y="82" label="Play Video" click="onSend()" enabled="false" id="playBtn"/>
- <s:Group id="canvas" x="237" y="111" width="222" height="249">
- </s:Group>
- <s:TextArea id="infoTxt" x="467" y="10" width="510" height="350"/>
- </s:Application>
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...