[AS3]adobe fms下p2p之实现图片分享功能源代码
[AS3]adobe fms下p2p之实现图片分享功能源代码中关于文件分享示例的改版,原文示例是基于flex的,我改成flash版本了(大致原理与上一篇完全相同
这其实是http://www.flashrealtime.com/file-share-object-replication-flash-p2p/ 中关于文件分享示例的改版,原文示例是基于flex的,我改成flash版本了(大致原理与上一篇完全相同):
有三个基本类:
1、P2PSharedObject.as 用于定义要分享的(图片)数据类
- package p2p
- {
- import flash.utils.ByteArray;
- public class P2PSharedObject
- {
- public var size:Number = 0;
- public var packetLenght:uint = 0;
- public var actualFetchIndex:Number = 0;
- public var data:ByteArray;
- public var chunks:Object = new Object();
- public function P2PSharedObject()
- {
- }
- }
- }
2、LocalFileLoader.as 用于浏览本地图片并加载到舞台,将自动将图片以约64000 byte左右为单位,分成一块一块
- package p2p
- {
- import flash.events.Event;
- import flash.events.EventDispatcher;
- import flash.events.IOErrorEvent;
- import flash.events.ProgressEvent;
- import flash.events.SecurityErrorEvent;
- import flash.events.StatusEvent;
- import flash.net.FileReference;
- import flash.utils.ByteArray;
- public class LocalFileLoader extends EventDispatcher
- {
- public function LocalFileLoader()
- {
- }
- private var file:FileReference;
- public var p2pSharedObject:P2PSharedObject;
- public function browseFileSystem():void {
- file = new FileReference();
- file.addEventListener(Event.SELECT, selectHandler);
- file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
- file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
- file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler)
- file.addEventListener(Event.COMPLETE, completeHandler);
- file.browse();
- }
- protected function selectHandler(event:Event):void {
- writeText("fileChosen");
- writeText(file.name+" | "+file.size);
- file.load();
- }
- protected function ioErrorHandler(event:IOErrorEvent):void {
- writeText("ioErrorHandler: " + event);
- }
- protected function securityErrorHandler(event:SecurityErrorEvent):void {
- writeText("securityError: " + event);
- }
- protected function progressHandler(event:ProgressEvent):void {
- var file:FileReference = FileReference(event.target);
- writeText("progressHandler: bytesLoaded=" + event.bytesLoaded + "/" +event.bytesTotal);
- }
- protected function completeHandler(event:Event):void {
- writeText("completeHandler");
- p2pSharedObject = new P2PSharedObject();
- p2pSharedObject.size = file.size;
- p2pSharedObject.packetLenght = Math.floor(file.size/64000)+1;
- p2pSharedObject.data = file.data;
- p2pSharedObject.chunks = new Object();
- p2pSharedObject.chunks[0] = p2pSharedObject.packetLenght+1;
- for(var i:int = 1;i<p2pSharedObject.packetLenght;i++){
- p2pSharedObject.chunks[i] = new ByteArray();
- p2pSharedObject.data.readBytes(p2pSharedObject.chunks[i],0,64000);
- }
- // +1 last packet
- p2pSharedObject.chunks[p2pSharedObject.packetLenght] = new ByteArray();
- p2pSharedObject.data.readBytes(p2pSharedObject.chunks[i],0,p2pSharedObject.data.bytesAvailable);
- p2pSharedObject.packetLenght+=1;
- writeText("----- p2pSharedObject -----");
- writeText("packetLenght: "+(p2pSharedObject.packetLenght));
- dispatchEvent(new Event(Event.COMPLETE));
- }
- protected function writeText(str:String):void{
- var e:StatusEvent = new StatusEvent(StatusEvent.STATUS,false,false,"status",str);
- dispatchEvent(e);
- }
- }
- }
热门文章推荐
- [FMS]Adobe FMS产品(Adobe Flash Media Server)使用介绍(组图)
- [FMS]Flash Media Server之教程附示例代码
- [RTMP]国产RTMP/HLS直播服务器simple-rtmp-server
- [WOWZA]用wowza流媒体服务器发布和播放直播流(RTMP)
- [RTMP]又一款rtmp类的服务器openrtmfp流媒体服务器
- [RTMP]adobe之FMS视频直播解决方案流程介绍(组图)
- [RTMP协议]实时消息传输协议 (RTMP) 常识性介绍
- [FMS]Adobe的Flash Media Server服务器(FMS服务器)安装
请稍候...