·您当前的位置:首页 > 技术教程 > Rtmp技术 >

[AS3]adobe fms下p2p之实现图片分享功能源代码(2)

时间:2014-05-08 08:46yjmyzz
3、P2PFileShare.as 用于处理P2P文件分享(即从一个peer端发送另一个peer端) packagep2p { importflash.events.Event; importflash.events.EventDispatcher; importflash.events.NetStatusEvent; importflash.events

3、P2PFileShare.as 用于处理P2P文件分享(即从一个peer端发送另一个peer端)

  1. package p2p 
  2.     import flash.events.Event; 
  3.     import flash.events.EventDispatcher; 
  4.     import flash.events.NetStatusEvent; 
  5.     import flash.events.StatusEvent; 
  6.     import flash.net.GroupSpecifier; 
  7.     import flash.net.NetConnection; 
  8.     import flash.net.NetGroup; 
  9.     import flash.net.NetGroupReplicationStrategy; 
  10.     import flash.utils.ByteArray;   
  11.       
  12.     public class P2PFileShare extends EventDispatcher 
  13.     {   
  14.           
  15.         public var connected:Boolean = false
  16.           
  17.         public var netConnection:NetConnection; 
  18.           
  19.           
  20.         public var netGroup:NetGroup; 
  21.           
  22.         private const SERVER:String = "rtmfp://localhost/"
  23.         private const DEVKEY:String = "HelloServer"
  24.           
  25.         public var p2pSharedObject:P2PSharedObject; 
  26.           
  27.         public function P2PFileShare() 
  28.         { 
  29.         } 
  30.           
  31.         public function connect():void{ 
  32.             netConnection = new NetConnection(); 
  33.             netConnection.addEventListener(NetStatusEvent.NET_STATUS,netStatus);                
  34.             netConnection.connect(SERVER+DEVKEY); 
  35.         } 
  36.           
  37.         public function startSharing(p2pSharedObject:P2PSharedObject):void{ 
  38.             triggerEvent("startSharing - chunks shared: "+p2pSharedObject.packetLenght);            
  39.             this.p2pSharedObject = p2pSharedObject;         
  40.             netGroup.addHaveObjects(0,p2pSharedObject.packetLenght); 
  41.         } 
  42.           
  43.         public function startReceiving():void{ 
  44.             triggerEvent("startReceiving");         
  45.             p2pSharedObject = new P2PSharedObject(); 
  46.             p2pSharedObject.chunks = new Object();          
  47.             receiveObject(0); 
  48.         } 
  49.           
  50.           
  51.         protected function onGroupConnected():void{ 
  52.             netGroup.replicationStrategy = NetGroupReplicationStrategy.LOWEST_FIRST; 
  53.               
  54.         } 
  55.           
  56.         protected function netStatus(event:NetStatusEvent):void{ 
  57.             triggerEvent(event.info.code); 
  58.               
  59.             switch(event.info.code){ 
  60.                 case "NetConnection.Connect.Success": 
  61.                     setupGroup(); 
  62.                     break; 
  63.                   
  64.                 case "NetGroup.Connect.Success": 
  65.                     connected = true
  66.                       
  67.                     onGroupConnected(); 
  68.                       
  69.                     break; 
  70.                   
  71.                 case "NetGroup.Replication.Fetch.SendNotify": // e.info.index 
  72.                     triggerEvent("____ index: "+event.info.index); 
  73.                       
  74.                     break; 
  75.                   
  76.                 case "NetGroup.Replication.Fetch.Failed": // e.info.index 
  77.                     triggerEvent("____ index: "+event.info.index); 
  78.                       
  79.                     break; 
  80.                   
  81.                 case "NetGroup.Replication.Fetch.Result": // e.info.index, e.info.object 
  82.                     //triggerEvent("____ index: "+event.info.index+" | object: "+event.info.object); 
  83.                       
  84.                     netGroup.addHaveObjects(event.info.index,event.info.index); 
  85.                       
  86.                     p2pSharedObject.chunks[event.info.index] = event.info.object; 
  87.                       
  88.                     if(event.info.index == 0){ 
  89.                         p2pSharedObject.packetLenght = Number(event.info.object); 
  90.                         triggerEvent("p2pSharedObject.packetLenght: "+p2pSharedObject.packetLenght); 
  91.                           
  92.                         receiveObject(1); 
  93.                           
  94.                     }else{ 
  95.                         if(event.info.index+1<p2pSharedObject.packetLenght){ 
  96.                             receiveObject(event.info.index+1); 
  97.                         }else{ 
  98.                             triggerEvent("Receiving DONE"); 
  99.                             triggerEvent("p2pSharedObject.packetLenght: "+p2pSharedObject.packetLenght); 
  100.                               
  101.                             p2pSharedObject.data = new ByteArray(); 
  102.                             for(var i:int = 1;i<p2pSharedObject.packetLenght;i++){ 
  103.                                 p2pSharedObject.data.writeBytes(p2pSharedObject.chunks[i]); 
  104.                             } 
  105.                               
  106.                             triggerEvent("p2pSharedObject.data.bytesAvailable: "+p2pSharedObject.data.bytesAvailable); 
  107.                             triggerEvent("p2pSharedObject.data.length: "+p2pSharedObject.data.length); 
  108.                               
  109.                             dispatchEvent(new Event(Event.COMPLETE)); 
  110.                         } 
  111.                     } 
  112.                       
  113.                       
  114.                     break; 
  115.                   
  116.                 case "NetGroup.Replication.Request": // e.info.index, e.info.requestID 
  117.                     netGroup.writeRequestedObject(event.info.requestID,p2pSharedObject.chunks[event.info.index]) 
  118.                     // 
  119.                       
  120.                     triggerEvent("____ ID: "+event.info.requestID+", index: "+event.info.index); 
  121.                     break; 
  122.                   
  123.                 default: 
  124.                     break; 
  125.             } 
  126.         } 
  127.           
  128.         protected function setupGroup():void{ 
  129.             triggerEvent("setupGroup");         
  130.             var spec:GroupSpecifier = new GroupSpecifier("myGroup"); 
  131.             spec.serverChannelEnabled = true
  132.             spec.objectReplicationEnabled = true;       
  133.             netGroup = new NetGroup(netConnection,spec.groupspecWithAuthorizations()); 
  134.             netGroup.addEventListener(NetStatusEvent.NET_STATUS,netStatus);         
  135.         } 
  136.           
  137.           
  138.         protected function receiveObject(index:Number):void{ 
  139.             netGroup.addWantObjects(index,index); 
  140.             p2pSharedObject.actualFetchIndex = index
  141.         } 
  142.           
  143.         protected function triggerEvent(str:String):void { 
  144.             trace("P2pFilShare.triggerEvent CuPlayer.com被调用:str->", str); 
  145.             var e:StatusEvent = new StatusEvent(StatusEvent.STATUS,false,false,"status",str); 
  146.               
  147.             dispatchEvent(e); 
  148.         } 
  149.           
  150.           
  151.           
  152.     } 

最后的flash peer端:

运行截图

  1. package { 
  2.   
  3.     import fl.controls.Button; 
  4.     import fl.controls.TextArea; 
  5.     import fl.controls.TextInput; 
  6.     import flash.display.StageAlign; 
  7.     import flash.display.StageScaleMode; 
  8.     import flash.events.Event; 
  9.     import flash.events.StatusEvent; 
  10.     import flash.display.Loader; 
  11.     import flash.display.MovieClip; 
  12.     import flash.events.MouseEvent; 
  13.     import flash.net.NetGroupInfo; 
  14.     import p2p.LocalFileLoader; 
  15.     import p2p.P2PFileShare; 
  16.     import p2p.P2PSharedObject; 
  17.   
  18.   
  19.     public class p2p_FileShare extends MovieClip { 
  20.   
  21.         private var _btnBrowse:Button; 
  22.         private var _btnShare:Button; 
  23.         private var _btnReceive:Button; 
  24.         private var _txtOutput:TextArea; 
  25.         private var _txtReceive:TextInput; 
  26.         private var _txtSend:TextInput; 
  27.   
  28.         private var _localFileLoader:LocalFileLoader; 
  29.         private var _loader:Loader; 
  30.         private var _fileShare:P2PFileShare; 
  31.   
  32.         public function p2p_FileShare(){ 
  33.             // constructor code         
  34.             init(); 
  35.         } 
  36.   
  37.         private function init():void { 
  38.             this.stage.align = StageAlign.TOP_LEFT; 
  39.             this.stage.scaleMode = StageScaleMode.NO_SCALE; 
  40.   
  41.             this._localFileLoader = new LocalFileLoader(); 
  42.             this._localFileLoader.addEventListener(StatusEvent.STATUS, onStatus); 
  43.             this._localFileLoader.addEventListener(Event.COMPLETE, fileLoaderComplete); 
  44.   
  45.             _fileShare = new P2PFileShare(); 
  46.             _fileShare.addEventListener(StatusEvent.STATUS, onStatus); 
  47.             _fileShare.addEventListener(Event.COMPLETE, fileShareComplete); 
  48.               
  49.   
  50.             this._fileShare.connect(); 
  51.             this._loader = new Loader(); 
  52.             addChild(_loader); 
  53.             _loader.x = 218
  54.             _loader.y = 43.35; 
  55.   
  56.             this._btnBrowse = btnBrowse; 
  57.             this._btnShare = btnStartShare
  58.             this._btnReceive = btnReceive; 
  59.             this._txtOutput = txtOutput; 
  60.             this._txtReceive = txtReceive; 
  61.             this._txtSend = txtSend; 
  62.   
  63.             this._btnBrowse.addEventListener(MouseEvent.CLICK, _btnBrowse_Click); 
  64.             this._btnReceive.addEventListener(MouseEvent.CLICK, _btnReceive_Click); 
  65.             this._btnShare.addEventListener(MouseEvent.CLICK, _btnShare_Click); 
  66.         } 
  67.   
  68.         private function onStatus(event:StatusEvent):void { 
  69.             writeText(event.level); 
  70.             if (event.level == "NetGroup.Connect.Success"){ 
  71.                 _btnShare.enabled = false
  72.                 _btnReceive.enabled = true
  73.             } 
  74.             try { 
  75.                 refreshInfo(); 
  76.             } catch (e:Error){ 
  77.   
  78.             } 
  79.         } 
  80.   
  81.         private function fileLoaderComplete(event:Event):void { 
  82.             writeText("fileLoaderComplete"); 
  83.             _loader.unload(); 
  84.             _loader.loadBytes(_localFileLoader.p2pSharedObject.data); 
  85.             this._fileShare.startSharing(this._localFileLoader.p2pSharedObject); 
  86.             this._btnShare.enabled = true
  87.             this._btnReceive.enabled = false
  88.             this._btnBrowse.enabled = false
  89.         } 
  90.   
  91.         private function fileShareComplete(event:Event):void { 
  92.             writeText("fileShareComplete"); 
  93.             _loader.unload(); 
  94.             _loader.loadBytes(_fileShare.p2pSharedObject.data); 
  95.         } 
  96.   
  97.         private function writeText(txt:String):void { 
  98.             trace("p2p_FileShare.writeText 被调用:txt->", txt); 
  99.             this._txtOutput.appendText(txt + "\n"); 
  100.         } 
  101.   
  102.         private function _btnBrowse_Click(e:MouseEvent):void { 
  103.             this._localFileLoader.browseFileSystem(); 
  104.         } 
  105.           
  106.         private function _btnShare_Click(e:MouseEvent):void { 
  107.             this._fileShare.startSharing(this._localFileLoader.p2pSharedObject); 
  108.             this._btnShare.enabled = false
  109.         } 
  110.           
  111.           
  112.         private function _btnReceive_Click(e:MouseEvent):void { 
  113.             this._fileShare.startReceiving(); 
  114.         } 
  115.   
  116.   
  117.         protected function refreshInfo():void { 
  118.             var obj:NetGroupInfo = this._fileShare.netGroup.info; 
  119.             _txtReceive.text = obj.objectReplicationReceiveBytesPerSecond + ""; 
  120.             _txtSend.text = obj.objectReplicationSendBytesPerSecond + ""; 
  121.         } 
  122.     } 
  123.   

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器