R – Flex 3 simple chat: code review request; is it good for a real life application

actionscript-3apache-flexchat

i managed to put together a simple chat using flex 3 / AS3, and i want to know if it is good for a real life application?

here is the mxml

<?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init();">
        <mx:Script source="code.as" />
        <mx:TitleWindow title="Shat">
        <mx:VBox>
        <mx:TextArea width="236" id="eShat" height="190"/>
            <mx:HBox>
                <mx:TextInput id="eMessage" enter="sendMessage()"/>
                <mx:Button label="Send" id="eSend"  />
                </mx:HBox>
        </mx:VBox>
        </mx:TitleWindow>
    </mx:Application>

and code in action script (code.as)

import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.events.SyncEvent;
import flash.net.NetConnection;
import flash.net.SharedObject;

var nc:NetConnection = null;
var so:SharedObject = null;

private var chatname:String = "mychat";

private function init():void
{
    if(nc == null)
    {
        nc = new NetConnection();       
        nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
        nc.connect("rtmp://localhost/simplu");
    }
}

private function netStatusHandler(event:NetStatusEvent):void 
{
    if (event.info.code == "NetConnection.Connect.Failed") 
    {
        trace(event.info.code);
    }
    if (event.info.code == "NetConnection.Connect.Rejected") 
    {
        trace(event.info.code);
    } 
    else if (event.info.code == "NetConnection.Connect.Success") 
    {
        so = SharedObject.getRemote(chatname, nc.uri, false);       
        so.addEventListener(SyncEvent.SYNC, syncHandler);               
        so.connect(nc);
        eSend.addEventListener(MouseEvent.CLICK, eSendClick);       
    } 
    else if (event.info.code == "NetConnection.Connect.Closed") 
    {
        trace(event.info.code);
    }
}

private function syncHandler(event:SyncEvent):void
{   
     if(so.data[chatname] != undefined) 
     {
        eShat.htmlText += so.data[chatname] + "\n";
        eShat.verticalScrollPosition = eShat.maxVerticalScrollPosition;
     }
}

private function eSendClick(event:Event):void
{
    sendMessage();
}

private function sendMessage():void
{
    so.setProperty(chatname, eMessage.text);    
    eMessage.text = "";
}

Best Answer

Check out the Chat sample in Tour de Flex for an example that uses BlazeDS or LCDS.

Related Topic