//Importing the necessary classes 导入需要的类
import mx.events.ItemClickEvent;
import mx.events.ListEvent;
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 = "################################";
//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
{
trace(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")
{
// 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
trace("completeHandler: " + event);
// The send button will be enabled
btnSend.enabled= true;
}
private function ioErrorHandler(event:IOErrorEvent):void
{
trace("ioErrorHandler: " + 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);
}
public function onFileSend(e:Event):void
{
// 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
{
// 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
{
// 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);
}
]]>