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

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

时间:2014-05-08 08:46yjmyzz
[AS3]adobe fms下p2p之实现图片分享功能源代码中关于文件分享示例的改版,原文示例是基于flex的,我改成flash版本了(大致原理与上一篇完全相同

这其实是http://www.flashrealtime.com/file-share-object-replication-flash-p2p/ 中关于文件分享示例的改版,原文示例是基于flex的,我改成flash版本了(大致原理与上一篇完全相同):

有三个基本类:

1、P2PSharedObject.as 用于定义要分享的(图片)数据类

  1. package p2p 
  2.     import flash.utils.ByteArray; 
  3.   
  4.     public class P2PSharedObject 
  5.     { 
  6.           
  7.         public var size:Number = 0
  8.         public var packetLenght:uint = 0
  9.         public var actualFetchIndex:Number = 0
  10.         public var data:ByteArray; 
  11.         public var chunks:Object = new Object(); 
  12.           
  13.         public function P2PSharedObject() 
  14.         { 
  15.         } 
  16.     } 

2、LocalFileLoader.as 用于浏览本地图片并加载到舞台,将自动将图片以约64000 byte左右为单位,分成一块一块

  1. package p2p 
  2.     import flash.events.Event; 
  3.     import flash.events.EventDispatcher; 
  4.     import flash.events.IOErrorEvent; 
  5.     import flash.events.ProgressEvent; 
  6.     import flash.events.SecurityErrorEvent; 
  7.     import flash.events.StatusEvent; 
  8.     import flash.net.FileReference; 
  9.     import flash.utils.ByteArray; 
  10.       
  11.       
  12.     public class LocalFileLoader extends EventDispatcher 
  13.     { 
  14.         public function LocalFileLoader() 
  15.         { 
  16.               
  17.         } 
  18.           
  19.           
  20.         private var file:FileReference; 
  21.         public var p2pSharedObject:P2PSharedObject; 
  22.           
  23.           
  24.         public function browseFileSystem():void { 
  25.             file = new FileReference(); 
  26.             file.addEventListener(Event.SELECT, selectHandler); 
  27.             file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); 
  28.             file.addEventListener(ProgressEvent.PROGRESS, progressHandler); 
  29.             file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler) 
  30.             file.addEventListener(Event.COMPLETE, completeHandler); 
  31.             file.browse(); 
  32.         } 
  33.           
  34.         protected function selectHandler(event:Event):void { 
  35.             writeText("fileChosen"); 
  36.               
  37.             writeText(file.name+" | "+file.size); 
  38.               
  39.             file.load(); 
  40.         } 
  41.           
  42.         protected function ioErrorHandler(event:IOErrorEvent):void { 
  43.             writeText("ioErrorHandler: " + event); 
  44.         } 
  45.           
  46.         protected function securityErrorHandler(event:SecurityErrorEvent):void { 
  47.             writeText("securityError: " + event); 
  48.         } 
  49.           
  50.         protected function progressHandler(event:ProgressEvent):void { 
  51.             var file:FileReference = FileReference(event.target); 
  52.             writeText("progressHandler: bytesLoaded=" + event.bytesLoaded + "/" +event.bytesTotal); 
  53.               
  54.         } 
  55.           
  56.         protected function completeHandler(event:Event):void { 
  57.             writeText("completeHandler"); 
  58.               
  59.             p2pSharedObject = new P2PSharedObject(); 
  60.             p2pSharedObject.size = file.size; 
  61.             p2pSharedObject.packetLenght = Math.floor(file.size/64000)+1; 
  62.             p2pSharedObject.data = file.data; 
  63.               
  64.               
  65.             p2pSharedObject.chunks = new Object(); 
  66.             p2pSharedObject.chunks[0] = p2pSharedObject.packetLenght+1; 
  67.             for(var i:int = 1;i<p2pSharedObject.packetLenght;i++){ 
  68.                 p2pSharedObject.chunks[i] = new ByteArray(); 
  69.                 p2pSharedObject.data.readBytes(p2pSharedObject.chunks[i],0,64000); 
  70.                   
  71.             } 
  72.             // +1 last packet 
  73.             p2pSharedObject.chunks[p2pSharedObject.packetLenght] = new ByteArray(); 
  74.             p2pSharedObject.data.readBytes(p2pSharedObject.chunks[i],0,p2pSharedObject.data.bytesAvailable); 
  75.               
  76.             p2pSharedObject.packetLenght+=1; 
  77.               
  78.             writeText("----- p2pSharedObject -----"); 
  79.             writeText("packetLenght: "+(p2pSharedObject.packetLenght)); 
  80.               
  81.               
  82.             dispatchEvent(new Event(Event.COMPLETE)); 
  83.         } 
  84.           
  85.         protected function writeText(str:String):void{ 
  86.             var e:StatusEvent = new StatusEvent(StatusEvent.STATUS,false,false,"status",str); 
  87.               
  88.             dispatchEvent(e); 
  89.         } 
  90.     } 

热门文章推荐

请稍候...

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

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