[转载]Flash P2P 文件共享基础教程

一篇很不错的讲解"利用flash player 10.1中的p2p特性实现文件共享"的文章,为防止原文被墙掉,转载于此,原始出处:http://www.flashrealtime.com/file-share-object-replication-flash-p2p/

Object Replication

Object Replication is the most lowest-level P2P access available in Flash Player 10.1 (followed by Multicast, Posting and Directed Routing). It basically enables you to send chunks of data between peers. Object Replication is the only P2P access method that guarantees that all data will be transferred to all receiving peers.

 

Demo

I’ve built this simple file sharing application, which basically loads a file and then you start sharing it. Open another client to start receiving the file.

How to use it:
Open a provider in one window – browse for a file (JPG, PNG, GIF). Once it’s loaded, it will start sharing the file. Open a receiver in many other windows and start receiving. Provider and receiver are included in one app in this example.

How does it work

A classic scenario for Object Replication in Flash is file sharing. You have two clients, one is sending the data (Provider) and the other one receives the data (Receiver). You were able to do this already in Flash Player 10 using NetStream – but this worked only for two clients and there where no replication of objects to the members of a group => Massive File Sharing! In our scenario, you can have thousands of receivers.

Provider
Provides data for others. This is the originator. First you need to have an object with data you want to share. You most probably will load a file using URLStream or FileReference. Then you need to split this file into separate ByteArray chunks and give them indexes (it can be an indexed array). Keep the chunks reasonably small to avoid transfer issues (around 64KB). So if you load a 2 MB file, you will have 32 chunks. Finally call NetGroup.addHaveObject(0, 32); which says you have in this case 32 chunks available for others.

Receiver
Receives data from a provider. Here you just call NetGroup.addWantObjects(index, index); and start receiving objects from the provider. I do this by keeping the increasing the index by 1 once received a chunk. So you basically call NetGroup.addWantObjects(index, index); 32 times. When you call addWantObjects, the Providers gets a status “NetGroup.Replication.Request”. At this point the provider needs to write data to a group using NetGroup.writeRequestedObject(event.info.requestID,chunks[event.info.index]). Once it writes the data, the Receiver gets a “NetGroup.Replication.Fetch.Result” status event and save the data locally to an object. Remember, that the Receiver is just receiving data, it is not providing the data to other peers. After it has received all chunks, the Receiver just goes through the chunks and put them together into a final ByteArray.

Receiver/Provider
So why not to provide the received data to other peers to make the trasfer faster and maybe more stable. Once you receive some data using NetGroup.addWantObjects(index, index); and save them in “NetGroup.Replication.Fetch.Result”, you can start providing the data to other peers using NetGroup.addHaveObject(index, index);.

Of course there is lot more to be done, but first let’s have a look at this schema for the above.

Simple Object Replication

To demonstrate how Object Replication works, let’s try this second demo. In this example you have an object, which we fill with and array of 100 elements. Then start sharing this array. Run the second client to start receiving the array.

Provider peer:
1. Once connected, click fillObject
2. Then click addHaveObjects
3. That’s all

Receiver peer:
1. Once connected, click addWantObjects
2. It should start receiving objects shortly
(there is a loop, first chunk you receive is count of objects, once you receive a chunk the index increases by 1 and asks for next chunk until they are all received)

There are couple more buttons – you can try playing with it a little bit if you want.

 

How was it built?

Once connected to a server setup a NetGroup instance like this:

private function setupGroup():void{
  var spec:GroupSpecifier = new GroupSpecifier("myGroup");
  spec.serverChannelEnabled = true;
  spec.objectReplicationEnabled = true; 
  netGroup = new NetGroup(netConnection,spec.groupspecWithAuthorizations());
  netGroup.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
}

Once connected to a NetGroup, which means that user allowed P2P connections, UDP is enabled and so on – you do operations on a NetGroup. First set object replication strategy. We will be receiving packets one by one (moreless).

netGroup.replicationStrategy = NetGroupReplicationStrategy.LOWEST_FIRST;

In your netStatusHandler – catch two codes:

// This code is called on a Provider
case "NetGroup.Replication.Request": 
  // calling this causes "NetGroup.Replication.Fetch.Result" invocation on a Receiver
  netGroup.writeRequestedObject(event.info.requestID,obj[event.info.index]) 
  break; 
// This code is called on a Receiver
case "NetGroup.Replication.Fetch.Result":        
  // received chunks can be already provided to others
  netGroup.addHaveObjects(event.info.index,event.info.index);         
  // write a chunk into an object/array
  obj[event.info.index] = event.info.object;
   if(event.info.index == 0){
    // First chunk (0) holds the number of chunks
    objSize = Number(event.info.object);}
  else{
    // Receive chunks until you are full
    if(event.info.index+1<objSize){
      netGroup.addWantObjects(event.info.index+1,event.info.index+1);
      actualFetchIndex = event.info.index+1;
    }
  }
  break;

The whole source code can be found here.

Creating ByteArray P2P File Sharing

By following the concept above it’s possible to load a file from disk or url and then start sharing it with others.

This explains how the first demo works.

For this I’ve split the application into four different classes:

LocalFileLoader.as
Loads a file using FileReference and splits it into chunks (~64 KB each).

P2PFileShare.as
Connects to Cirrus and handles all Object Replication sending and receiving

P2PSharedObject.as
A simple value object, which holds the data (ByteArray), size, packetLenght, actualFetchIndex and chunks; it’s used by both classes above.

P2PFileSharing.mxml
The user interface, which puts it all together.

The complete source code can be found here.

The Provider should look like this after sending the data:

 

The Receiver should look like this after receiving the data:

 

In the next tutorial, I will look at how to use Object Replication with VOD video.

Where to go from here

Check other tutorials on P2P in Flash:

- Video-on-Demand over P2P in Flash Player 10.1 with Object Replication
- P2P GroupSpecifier Class Explained In Details Part 1
- Multicast Explained in Flash 10.1 P2P
- Directed Routing Explained in Flash 10.1 P2P
- Simple chat with P2P NetGroup in FP 10.1

Video tutorials:
- P2P Chat with NetGroup in Flash Player 10.1
- Multicast Streaming in Flash Player 10.1 Tutorial

时间: 2024-09-21 19:58:15

[转载]Flash P2P 文件共享基础教程的相关文章

Asp与Flash结合开发应用基础教程

1.Flash与Asp之间的交互2.Asp与数据库之间的交互3.ASP+FLASH开发源码例子――登陆的实现 Flash 的功能已经非常强大,完全可以跟其他的服务器端语言结合起来,做出适合现在的网络应用的网站.一个典型的模式就是Flash 与Asp的结合使用.其架构如图1所示. 图01 可以把上面的结构看成是三层,Flash是一层,Asp是一层,数据库是一层.对Flash和Asp有点了解的人,只要处理好层与层之间的交互,结合使用Flash与Asp就决不是什么难事. 一.下面我们就先来看看Flas

Flash动画基础教程①入门篇

flash动画|基础教程 這個教程是本人的一些經驗之談,為了初學者的入門教程,未經本人許可請勿轉載!謝謝 Flash动画基础教程--入门篇①首先我们来说说什么是Flash? Flash是Macromedia公司出品的软件,对于Flash是什么很多的描述,大致是"交互性矢量多媒体制作软件"的意思!首先,它是一种多媒体制作软件,Flash的产品往往都是一些"有声有色"给人视觉冲击的动画.其次,Flash是以矢量图为基础的,矢量图最大的特点就是它能无限的缩放,不会因为图像

初学者的FLASH基础教程

初学|基础教程 大家都知道,FLASH以流控制技术和矢量技术等为核心,制作出的动画具有短小精悍的特点,被广泛用于网页动画设计,成为当今网页动画制作最为流行的软件.好了,废话不说了,转为正题吧! 一.基本概念 1.矢量图和位图 矢量图是由计算机根据矢量数据计算后绘制而成的,它由线条和色块组成.它具有如下特点:一是文件的大小与图形的复杂程度有关,但是与图形的尺寸无关:二是图形的显示尺寸可以进行无极限的缩放,缩放程度不影响图形的显示精度和效果. 位图是由计算机显示器上的行扫描和列扫描点阵组成的,每个扫

Flash ActionScript 2.0 基础教程

基础教程 本文译者:egoldy文章出处: http://www.ultrashock.com 文章性质:翻译 ActionScript 2.0-简介 随着FLASHMX2004的推出,Macromedia公司推出了一种新的脚本类型称为as2,在要教程中我们将要了解AS2的新特性.新的面向对象的模型以及如何使AS1的脚本过度到AS2. 为什么我们需要一种新的脚本语言. 如果你只是做一些简单的动画是不需要用一种新的脚本语言的,其实有许多项目是不需要脚本语言的.如果你真的不需要使用脚本语言那这个教程

WEB标准建站-XHTML基础教程2

html基础|web|web标准|xhtml|基础教程 本站原创内容,转载请注明出处网页教学网. XHTML DTD定义文档的类型. 在XHTML中我们必须声明文档的类型,以便于浏览器知道你的文档是什么类型的,而且声明部分要加在文档的head之前.如: <!DOCTYPE Doctype goes here> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Title g

WEB标准建站-XHTML基础教程1

html基础|web|web标准|xhtml|基础教程 本站原创内容,转载请注明出处网页教学网. 前言: 现在都讲究标准建站,而标准建站使用的技术主要是XHTML+CSS,而现在我们普遍使用的是HTML代码,那么我该如何转换呢?以及HTML和XHTML有什么不同呢?在这个教程里,你将学到HTML和XHTML之间的不同,以及如何将HTML转换为XHTML.毕竟XHTML是发展的方向,所以我觉得该教程有必要在本站上发布.我感觉要是你想使用标准还是最好先学HTML,因为比较简单,然后再来看该教程. X

AJAX开发基础教程篇

ajax|基础教程 一. 什么是AJAX? 这个名字代表了异步JavaScript+XMLHTTPRequest,并且意味着你可以在基于浏览器的JavaScript和服务器之间建立套接字通讯.其实AJAX并不是一种新技术,而是已经成功地用于现代浏览器中的若干成功技术的可能性组合.所有的AJAX应用程序实现了一种"丰富的"UI--这是通过JavaScript操作HTML文档对象模型并且经由XMLHttpRequest实现的精确定位的数据检索来实现的.典型的示例AJAX应用程序是Googl

Ajax与《Ajax基础教程》

ajax|基础教程 <Ajax基础教程>是国际上第一部有关Ajax的图书.其中文版也是国内首部Ajax图书,即将由人民邮电出版社推出. <Ajax基础教程>图灵程序设计丛书 (美)Ryan Asleson Nathaniel T.Schutta著金灵等译 人民邮电出版社2006年1月 定价:35元 Ajax,Ajax,Ajax!请不要弄错了,我说的不是希腊史诗中的英雄,也不是那支享誉全球的梦幻荷兰足球俱乐部,更不是加拿大的某个地名.去问问身边的技术追新族,他会一脸不屑地告诉你:连A

PHP新手上路基础教程目录

基础教程 PHP新手上路基础入门教程目录清单,本教程由网页教学网收集整理希望对初学PHP的朋友有所帮助,当您转载时别忘了加上本站的连接,谢谢! PHP新手上路基础简介 为什么要选择PHP 从一个简单的程序来了解PHP PHP入门之数据类型 建设一个简单交互的网站(一) 用include和require进行模块化,从HTML到PHP 建设一个简单交互的网站(二) 计数器 反馈表单 站内搜索引擎 建设一个简单交互的网站(三) 密码验证 建设一个简单交互的网站(四) 文件上传 建设一个简单交互的网站(