Skip to content

Instantly share code, notes, and snippets.

@shamun
Created July 27, 2010 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shamun/492871 to your computer and use it in GitHub Desktop.
Save shamun/492871 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
private const SERVER_ADDRESS:String = "rtmfp://status.adobe.com/";
private const DEVELOPER_KEY:String = "my_developer_code";
private var nc:NetConnection;
private var myPeerID:String;
private var farPeerID:String;
// streams
private var sendStream:NetStream;
private var recvStream:NetStream;
private function initConnection():void{
if(txtFingerprint.text){
farPeerID = txtFingerprint.text;
}
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS,ncStatus);
nc.connect(SERVER_ADDRESS+DEVELOPER_KEY);
}
private function ncStatus(event:NetStatusEvent):void{
trace(event.info.code);
myPeerID = nc.nearID;
txtFingerprint.text = myPeerID;
}
private function initSendStream():void{
trace("initSendStream");
sendStream = new NetStream(nc,NetStream.DIRECT_CONNECTIONS);
sendStream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
sendStream.publish("media");
var sendStreamClient:Object = new Object();
sendStreamClient.onPeerConnect = function(callerns:NetStream):Boolean{
farPeerID = callerns.farID;
trace("onPeerConnect "+farPeerID);
return true;
}
sendStream.client = sendStreamClient;
}
private function initRecvStream():void{
recvStream = new NetStream(nc,farPeerID);
recvStream.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
recvStream.play("media");
recvStream.client = this;
}
public function receiveSomeData(str:String):void{
txtReceiveData.text = str;
}
private function sendSomeData():void{
sendStream.send("receiveSomeData",txtSendData.text);
}
private function netStatusHandler(event:NetStatusEvent):void{
trace(event.info.code);
}
]]>
</mx:Script>
<mx:TextInput x="10" y="10" width="391" id="txtFingerprint"/>
<mx:Button x="409" y="10" label="Connect" click="initConnection()"/>
<mx:TextInput x="10" y="40" id="txtSendData"/>
<mx:TextInput x="10" y="70" id="txtReceiveData" width="251"/>
<mx:Button x="178" y="40" label="Send data" click="sendSomeData()"/>
<mx:Button x="10" y="100" label="initSendStream" click="initSendStream()"/>
<mx:Button x="132" y="100" label="initReceiveStream" click="initRecvStream();"/>
<mx:Text x="10" y="130" text="Hint: First running Flash app - click Connect to get Fingerprint PeerID. Copy and paste this PeerID to second running Flash app to the same field and click Connect. Then initSendStream and initReceiveStream on both of them and finally you can write some text and click Send data." width="391" height="122"/>
</mx:Application>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment