您的位置:首页 > 其它

利用Flex实现P2P文件传送

2017-10-10 16:47 141 查看
mxml代码:

<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" addedToStage="init()">

 <mx:Script
source="test/filesend.as">
</mx:Script>

 <mx:TextInput x="9" y="41"
width="301" id="txtNearID"/>

 <mx:TextInput x="9" y="72"
width="301" id="txtFarID"/>

 <mx:Label x="319" y="72"
text="Target address"/>

 <mx:Label x="319" y="42"
text="Your address"/>

 <mx:Button x="237" y="102"
label="Validate" click="receive(event)"/>

 <mx:VBox  x="8"
y="165" width="302" height="242" id="fileField"
horizontalAlign="right">

  <mx:HBox
width="100%" height="28">

   <mx:TextInput
width="227" id="txtFile"/>

   <mx:Button
label="Browse" click="browseHandler()"/>

  </mx:HBox>

  <mx:List
width="302" height="143" id="fileList"
></mx:List>

  <mx:Button
id="btnSend" label="Send" enabled="false"
click="onFileSend(event)"/>

 </mx:VBox>

</mx:Application>

 

as代码:

//Importing the
necessary classes

import mx.events.ItemClickEvent;

import mx.events.ListEvent;

import mx.controls.Alert;

//import mx.automation.events.ListItemSelectEvent;

import mx.collections.ArrayCollection;

// Creating constants
for the stratus serveraddress and your developer key

private const SERVER_ADDRESS:String =
"rtmfp://stratus.adobe.com/";

private const DEVELOPER_KEY:String =
"d1206cd8a9de7cd4b0b960e3-73bf82f0a3dd";

//Creating variable for
handling the incomming and outgoing data

private var netConnection:NetConnection;

private var sendStream:NetStream;

private var recievedStream:NetStream;

// Create a variabele
for handling the files

private var file:FileReference;

// A Dataprovider for
the listitem (to display downloads)

private var dp:ArrayCollection = new ArrayCollection;

private function
init():void

{

 // Creating a new connection to the Stratus
server

 netConnection = new NetConnection();

 netConnection.addEventListener(NetStatusEvent.NET_STATUS,
netConnectionHandler);

 netConnection.connect(SERVER_ADDRESS+DEVELOPER_KEY);

}

public function
netConnectionHandler(e:NetStatusEvent):void



 // If there was a connection was made with the
Stratusserver

 if(e.info.code ==
"NetConnection.Connect.Success")

 {

  // Adding your id to the NearID
textfield

  txtNearID.text =
netConnection.nearID;

  // Creating a new netstream
that handles the outgoing data

  sendStream = new
NetStream(netConnection, NetStream.DIRECT_CONNECTIONS);

  sendStream.addEventListener(NetStatusEvent.NET_STATUS,
netStreamHandler);

  // Giving the stream a
idName

  sendStream.publish("file");

 }

}

public function
netStreamHandler(e:NetStatusEvent):void

{

 Alert.show(e.info.code);

}

private function
receive(e:Event):void



 // This functions handles the incomming
data

 // Connection between you and the target

 recievedStream = new
NetStream(netConnection,txtFarID.text);

 recievedStream.client = this;

 recievedStream.addEventListener(NetStatusEvent.NET_STATUS,
incomingStreamHandler);

 // Using the "file" stream identification

 recievedStream.play("file");

}

public function
incomingStreamHandler(e:NetStatusEvent):void

{

 

 // If there was a connection between you and the
target

 if(e.info.code == "NetStream.Play.Start")

 {

  Alert.show(e.info.code);

  // set the textBackgroundcolor
to green

  txtFarID.setStyle("backgroundColor",
"#00ff00");

  // show the fileBrowse/Send
view

  fileField.visible = true;

 }

}

private function
browseHandler():void

{

 // Creating a new FileReference to start choosing
your file

 file = new FileReference;

 // Adding eventListeners

 file.addEventListener(Event.SELECT,
selectHandler);

 file.addEventListener(IOErrorEvent.IO_ERROR,
ioErrorHandler);

 file.addEventListener(ProgressEvent.PROGRESS,
progressHandler);

 file.addEventListener(Event.COMPLETE,
completeHandler);

 // Open the browse-file dialogue

 file.browse();

}

private function
selectHandler(event:Event):void

{

 // If you selected a file to send

 file = 
FileReference(event.target);

 // load the file

 file.load();

 // Set the source textfield to the filename

 txtFile.text = file.name;

}

private function completeHandler(event:Event):void

{

 // The file was loaded succesfully

 //Alert.show("completeHandler: " + event);

 //trace("completeHandler: " + event);

 // The send button will be enabled

 btnSend.enabled= true;

}

private function
ioErrorHandler(event:IOErrorEvent):void

{

 trace("ioErrorHandler: " + event);

 //Alert.show("出错了: " + event);

}

private function
progressHandler(event:ProgressEvent):void

{

 // Process  loading the
file

 var file:FileReference =
FileReference(event.target);

 //trace("progressHandler: name=" + file.name + "
bytesLoaded=" + event.bytesLoaded + " bytesTotal=" +
event.bytesTotal);

 //Alert.show("progressHandle提交: name=" +
file.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" +
event.bytesTotal);

 //Alert.show("chengong");

 

}

public function
onFileSend(e:Event):void

{

 Alert.show("send: " + e);

 // Creating an object

 var fileData:Object = new Object();

 // Giving the object a property that container
the file data as a ByteArray

 fileData.file = file.data

 // Property of the name of the file

 fileData.name = file.name;

 // Sending the file to the target and it will be
received

 // by the 'onFileReceived' function

 sendStream.send("onFileReceived",fileData);

}

public function
onFileReceived(info:Object):void

{

 Alert.show("888");

 // This functions handles the incomming
data

 // We add the object to a dataprovider

 // For displayin the files in a list

 // Giving the name and file data as
properties

 dp.addItem({label:info.name,
file:info.file});

 // If an item of the list was clicked

 fileList.addEventListener(ListEvent.ITEM_CLICK,
itemDownloadHandler);

 // Setting the dataprovider of the
listcomponent

 fileList.dataProvider = dp;

}

private function itemDownloadHandler(event:ListEvent):void

{

 Alert.show("999999999");

 // creating new FileReference

 file= new FileReference();

 // saving the clicked item to your computer

 // brings out a savedialogue

 file.save(event.itemRenderer.data.file,
event.itemRenderer.data.label);

}

说明:

FP10里,AS3提供了一个新的API用于支持RTMFP协义。连接到Stratus服务并创建点到点的流媒体就像是在FMS上工作一般。需要使用的的开发工具是Flash
CS4或更高版本和Flash Builder4,运行环境需要在FP10以上或AIR1.5上。

1、首先需要连接到Stratus上,代码:

private const StratusAddress:String =
"rtmfp://stratus.rtmfp.net";
private const DeveloperKey:String =
"your-developer-key";
private var
netConnection:NetConnection; 
netConnection = new NetConnection();

netConnection.addEventListener(NetStatusEvent.NET_STATUS,netConnectionHandler);
netConnection.connect(StratusAddress + "/" +
DeveloperKey);

其中DeveloperKey可在adobe
labs上免费申请:http://labs.adobe.com/technologies/stratus/

如果能正常连上Stratus,会得到NetConnection.Connect.Success事件。如果key无效,会得到NetConnection.Connect.Failed事件。如果防火墙阻挡了UDP通信,也会收到NetConnection.Connect.Failed事件。

2、在成功连接上Stratus之后,会获得一个256位的唯一标识符peerID(NetConnection.nearID)。其他FP端需要知道这个ID,用于接收你发布的音频/视频流。Stratus并不提供用户系统,所以这个ID一般还要跟你自身的进行转换。可以使用xmpp协义或是一个简单的webService来实现这个ID交换。

3、FP实例之间使用NetStream进行单向的直接通讯。如果要进行双向语音交流,那每个FP端必须建立两个NetStream,一个用于发送,一个用于接收。
创建发送流:

private var
sendStream:NetStream; 
sendStream = new NetStream(netConnection,
NetStream.DIRECT_CONNECTIONS);

sendStream.addEventListener(NetStatusEvent.NET_STATUS,netStreamHandler);
sendStream.publish("media");
sendStream.attachAudio(Microphone.getMicrophone());
sendStream.attachCamera(Camera.getCamera());

其中publish的参数media表示发布音频/视频流的名称,Stratus并不支持媒体服务,只能发布点到点的媒体流。当发送媒体流时,FP只发送给那些监听了该流的播放器。
4、创建接收流

private var recvStream:NetStream; recvStream = new
NetStream(netConnection, id_of_publishing_client);
recvStream.addEventListener(NetStatusEvent.NET_STATUS,
netStreamHandler); recvStream.play("media");
在接收媒体流时,需要知道发布方的id。
5、发送者会发送一些控制信息给接收者。当接收者尝试获得该发布流时,发送者可以在onPeerConnect里决定是否允许接收者接收。
var o:Object = new
Object(); o.onPeerConnect =
function(subscriberStream:NetStream):Boolean { if (accept) { return
true; } else { return false; } } sendStream.client =
o;
6、在发布端的NetStream.peerStreams属性包含有了所有监听该流的接收者,可以通过sendStream.send()将流发布给所有监听者,也可以通过sendStream.peerStreams[i].send()将流发给指定的监听者。
7、NetConnection.maxPeerConnections属性是允许接收该流的最大连接数。默认值是8.
 
关键点:运行步骤:
1.发送端A进入系统:“系统提示链接成功”,得到peerID,
2.接收端B进入系统:“系统提示链接成功”,得到peerID,
3.复制发送端的peerID(非常重要,一定要发送端的),粘贴到接收端的的验证框,点击validate按钮验证,提示“验证成功”
4.在发送端系统有提示"验证成功",确定,
5.上传要发送的文件,
6.点击发送
7.在接收端看到发过来的文件,点击文件名即可保存!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: