Red5 Chat in Flex 4 – Gumbo

Apr 12, 2010   //   by admin   //   ActionScript 3.0, Flash, Flash Develop, Flex, Red5  //  No Comments

Een simple chat aplicatie met red5 en flex4 sdk with flashdevelop.. Het is leuk om hiermee te starten en verder ontwikkellen tot een uitgebeide chat applicatie…

hier is de mxml code:
<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”
minWidth=”1024″ minHeight=”768″
initialize=”TextSO()”>

<!– Messaging Declarations –>

<!– UI Declarations –>

<s:Panel title=”Simple Flex Chat”>
<mx:TextArea id=”textAreaChat” valueCommit=”textAreaChat.verticalScrollPosition=textAreaChat.maxVerticalScrollPosition”
width=”385″ height=”220″ y=”20″
>
</mx:TextArea>
<s:TextInput id=”textInput” width=”385″  />
<mx:ControlBar horizontalAlign=”center” width=”385″>
<s:Button id=”button” label=”Send” click=”sendMsg(event)” />
</mx:ControlBar>
</s:Panel>

<!– Event-Handling Script –>

<fx:Script>
<![CDATA[
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.SyncEvent;
import flash.events.NetStatusEvent;
import flash.events.MouseEvent;
import flash.net.SharedObject;
import flash.net.NetConnection;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import mx.events.ChildExistenceChangedEvent;

private var outputText:TextField;
//private var button:Button;
private var text_so:SharedObject;
private var nc:NetConnection;
//private var textArea:TextArea;
//private var textInput:TextInput;
private var rtmpGo:String;
private var good:Boolean;
private var noName:Boolean;
//private var msg:String = "msg";

public function TextSO ():void
{
/* //Set up UIs
textArea=new TextArea();
textArea.setSize (200,300);
textArea.move (20,20);
addChild (textArea);

textInput=new TextInput();
textInput.move (20,330);
addChild (textInput);

button=new Button();
button.width=50;
button.label="Send";
button.move (125,330);*/
//button.addEventListener (MouseEvent.CLICK,sendMsg);
//addChild (button);
/*outputText = new TextField();
outputText.x = 10;
outputText.y = 10;
outputText.background = true;
outputText.autoSize = TextFieldAutoSize.LEFT;
addChild(outputText);*/

textInput.addEventListener(KeyboardEvent.KEY_DOWN,keyHandler);

//textInput.setFocus();

rtmpGo = "rtmp://87.211.215.87/test1";
nc = new NetConnection( );
nc.connect (rtmpGo);
nc.addEventListener (NetStatusEvent.NET_STATUS,doSO);
}

private function keyHandler(event:KeyboardEvent):void {

var curKeyCode:int = event.keyCode;
if (curKeyCode == 13) {
sendMsg1();
}

}

private function doSO (e:NetStatusEvent):void
{
good=e.info.code == "NetConnection.Connect.Success";
if (good)
{
trace(e.info.code);
//Set up shared object
text_so=SharedObject.getRemote("test",nc.uri,false);
text_so.connect (nc);
text_so.addEventListener (SyncEvent.SYNC, checkSO);

}
}
private function checkSO (e:SyncEvent):void
{
for (var chng:uint; chng<e.changeList.length; chng++)
{
switch (e.changeList[chng].code)
{
case “clear” :
break;

case “success” :
trace (text_so.data.msg);
break;

case “change” :
textAreaChat.text += text_so.data.msg + “\n”;
//textAreaChat.appendText(text_so.data.msg + “\n”);
break;
}
}
}

private function sendMsg (e:MouseEvent):void
{
noName= (textInput.text==”");
if (noName)
{

}else{
//textAreaChat.htmlText += textInput.text + “\n”;
text_so.setProperty (“msg”, textInput.text);
//textAreaChat.appendText (textInput.text + “\n”);
textAreaChat.text += textInput.text + “\n”;

textInput.text = “”;
}
}

private function sendMsg1 ():void
{
noName= (textInput.text==”");
if (noName)
{

}else{

text_so.setProperty (“msg”,textInput.text);
textAreaChat.text += textInput.text + “\n”;
//outputText.appendText (textInput.text + “\n”);
textInput.text = “”;
}
}
]]>
</fx:Script>

</s:Application>

Leave a comment