<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontSize="12" creationComplete="init()" viewSourceURL="srcview/index.html">

<mx:Script>
    <![CDATA[
    
    import vo.Message;
    import mx.collections.ArrayCollection;
    import mx.controls.Alert;
    
    private var myNetConnection:NetConnection;
    private var serverApp:String ="rtmp://127.0.0.1/test_myApp";
    private var talk_so:SharedObject;
        
    private function init():void
    {
        btn_send.addEventListener(MouseEvent.CLICK,btnSenClickHandler);
        myNetConnection = new NetConnection();
        myNetConnection.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);    
        myNetConnection.connect(serverApp);
    }
    
    private function netStatusHandler(evt:NetStatusEvent):void
    {
        trace(evt.info.code);  //调试代码用
        
        if ( evt.info.code =="NetConnection.Connect.Success" )
        {
            talk_so = SharedObject.getRemote("talk",myNetConnection.uri,true);
            talk_so.addEventListener(SyncEvent.SYNC,talkSoSyncHandler);
            talk_so.connect(myNetConnection);
        }
        else
        {
            Alert.show("連接失敗"+evt.info.code);
        }
    }
    
    private function talkSoSyncHandler(evt:SyncEvent):void
    {
        txt_content.text="";
        if ( talk_so.data.msgList!=null )
        {
            var tmp:ArrayCollection = new ArrayCollection();
            convertArrayCollection(tmp,talk_so.data.msgList as ArrayCollection);
            
            for(var i:int=0;i<tmp.length ;i++)
            {
                var message:Object = tmp.getItemAt(i);
                
                var fullMsg:String=message.nickname+"在"+message.time.toTimeString()+"說:"+message.msg;
                
                txt_content.text=txt_content.text+fullMsg+"\n";
            }
        }
    }
        
    private function btnSenClickHandler(evt:MouseEvent):void
    {
        var arr:ArrayCollection = new ArrayCollection();
        
        if ( talk_so.data.msgList==null )
        {
            arr = new ArrayCollection();    
        }
        else
        {
            convertArrayCollection(arr,talk_so.data.msgList as ArrayCollection);
        }
        
        var obj:Message = new Message();
        obj.nickname=txt_nickname.text;
        obj.msg=txt_message.text;
        obj.time = new Date();
        
        arr.addItem(obj);
        
        talk_so.setProperty("msgList",arr);
        txt_message.text="";
    }
    
    private function convertArrayCollection(arrNew:ArrayCollection,arrOld:ArrayCollection):void
    {
        arrNew.removeAll();
        
        for(var i:int=0;i<arrOld.length ;i++)
        {
            arrNew.addItemAt(arrOld.getItemAt(i),i);
        }
    }
    
        
    ]]>
</mx:Script>


    <mx:TextArea x="33" y="10" height="159" width="366" id="txt_content"/>
    <mx:TextInput x="33" y="177" width="62" id="txt_nickname"/>
    <mx:Label x="103" y="179" text=""/>
    <mx:TextInput x="146" y="177" width="185" id="txt_message"/>
    <mx:Button x="334" y="177" label="send" id="btn_send"/>


    
</mx:Application>