Skip to content

Instantly share code, notes, and snippets.

@FrancisVarga
Created July 5, 2010 15:44
Show Gist options
  • Save FrancisVarga/464477 to your computer and use it in GitHub Desktop.
Save FrancisVarga/464477 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="822" height="672"
creationComplete="onCreationComplete(event)">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
private const SERVER_ADDRESS:String = "rtmfp://stratus.adobe.com/";
private const DEVELOPER_KEY:String = "YOUR-KEY";
private var netConnection : NetConnection = new NetConnection();
private var inputStream : NetStream;
private var outputStream : NetStream;
private var farPeerID : String;
[Bindable]private var myPeerID : String;
private var camera : Camera;
private var microphone : Microphone;
private function onCreationComplete(event : FlexEvent):void{
xTrace("onCreationComplete", "onCreationComplete");
netConnection.addEventListener(NetStatusEvent.NET_STATUS, onNetStatusListener);
netConnection.connect(SERVER_ADDRESS+DEVELOPER_KEY);
attachWebCam();
attachMic();
}
private function attachMic():void{
xTrace("get microphone", "attachMic");
microphone = Microphone.getMicrophone();
}
private function attachWebCam():void{
camera = Camera.getCamera();
selfVideo.attachCamera(camera);
}
private function onNetStatusListener(event : NetStatusEvent):void{
xTrace("Status => " + event.info.code, "onNetStatusListener");
myPeerID = netConnection.nearID;
switch(event.info.code){
case "NetConnection.Connect.Success" :
connectStreams();
break;
}
}
private function connectStreams():void{
trace("initSendStream");
outputStream = new NetStream(netConnection, NetStream.DIRECT_CONNECTIONS);
outputStream.addEventListener(NetStatusEvent.NET_STATUS, onNetStatusListener);
outputStream.publish("media");
var sendStreamClient:Object = new Object();
sendStreamClient.onPeerConnect = function(callerns:NetStream):Boolean{
farPeerID = callerns.farID;
trace("onPeerConnect => " + farPeerID);
return true;
};
outputStream.client = sendStreamClient;
}
private function xTrace(message : String, functionName : String = ""):void{
trace("[" + getTimer() + "].["+functionName+"] => " + message);
}
protected function connectToRemoteBtn_clickHandler(event:MouseEvent):void
{
if(faarID.text.length != 64){
Alert.show("You have a wrong ID! Got To hell!", "Error! DAU USER");
} else {
farPeerID = faarID.text;
xTrace("Connect with => " + farPeerID);
inputStream = new NetStream(netConnection, farPeerID);
inputStream.play("media");
inputStream.client = this;
outputStream.attachCamera(camera);
outputStream.attachAudio(microphone);
var remoteTempVideo : Video = new Video();
remoteTempVideo.width = 320;
remoteTempVideo.height = 240;
remoteTempVideo.attachNetStream(inputStream);
remoteVideo.addChild(remoteTempVideo);
}
}
public function receiveSomeData(str:String):void{
xTrace(str);
addChatMessage(str, "Remote");
}
protected function chatInputTxt_keyDownHandler(event:KeyboardEvent):void
{
if(event.keyCode == 13){
if(chatInputTxt.text.length > 0){
addChatMessage(chatInputTxt.text, "You");
outputStream.send("receiveSomeData", chatInputTxt.text);
chatInputTxt.text = "";
} else {
return;
}
}
}
private function addChatMessage(message : String, from : String):void{
chatOutputTxt.text += "["+from+"] => " + message;
}
]]>
</fx:Script>
<s:TextInput id="faarID" x="10" y="50" width="473"/>
<s:Button id="connectToRemoteBtn" x="491" y="50" label="Connect" click="connectToRemoteBtn_clickHandler(event)"/>
<mx:VideoDisplay id="remoteVideo" x="10" y="79" width="388" height="310"/>
<mx:VideoDisplay id="selfVideo" x="401" y="80" width="160" height="160"/>
<s:TextArea id="chatOutputTxt" editable="false" x="10" y="397" width="551" height="226"/>
<s:TextArea id="chatInputTxt" editable="true" x="10" y="631" width="551" height="27" borderColor="0xFF4433FF" keyDown="chatInputTxt_keyDownHandler(event)"/>
<s:TextArea id="ownPeerID" text="{myPeerID}" x="10" y="10" width="548" height="32" editable="false"/>
<s:Group x="566" y="10" width="246" height="652">
<s:Label x="0" y="6" text="Userliste" width="246" height="16" fontSize="18" fontWeight="bold" textAlign="center"/>
<s:VGroup id="userListContainer" x="23" y="30" width="200" height="612">
</s:VGroup>
</s:Group>
</s:Application>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment