tags:

views:

408

answers:

0

Trying to understand the basic role server side logic plays in FMS apps.

For instance, most tutorials on simple text chat have server side code in .asc files. But I've seen one example that had none. What is lost without it?

I'm testing a CDN that doesn't allow access to FMS applications folder for test accounts and I'd like to deploy and test chat among other things.

I see an example in chapter 4 of the "Learning Flash Media Server" document that doesn't make mention of server side logic or a main.asc file, and seems to do all of its work client side.

It simply appears to declare and manipulate a shared object in the client. It seems that when new text entered by the user is added to a property in the shared object, a SyncEvent fires and propagates the change to other running clients, provided of course the handler code is written properly. Am I on track here? Or is there something critical that gets lost without some server side logic?

Chapter 4 of the document "Learning Flash Media Server" comes with a simple text chat example done in Flash. I took that code and translated to Flex 3. My code is below.

It appears to work on my local machine. Run two browsers and it works. Deploy it to a web server though and run from different machines and results are unreliable. Works sometimes and sometimes not. Send 10 messages and only 5 make it.

I'm not sure which is incorrect: my code or my assumption about not requiring server side code.

My code simply takes user input when the user hits a send button and adds it to a property in a shared object. It's sample code that I pretty much left in tact. Just changed the interface to use Flex 3 components.

Here's the code (although I see in the preview window that it's leaving out my "Application" tag code. Not sure why.)

<mx:Script>
 <![CDATA[

  import mx.events.FlexEvent;
  import flash.events.SyncEvent;
     import flash.events.NetStatusEvent;
     import flash.events.MouseEvent;
     import flash.events.FocusEvent;
     import flash.net.SharedObject;
     import flash.net.NetConnection;



  private var text_so:SharedObject;
  private var nc:NetConnection;
  private var ns:NetStream;   
  private var defaultServerURL:String;
  private var good:Boolean;
  private var catchKey:Boolean;
  private var noName:Boolean; 


  private function onCreationComplete():void
  {
   defaultServerURL = "rtmp://localhost/chat";    

   nc = new NetConnection( );
            nc.client = this;    
   nc.connect(defaultServerURL);
   nc.addEventListener(NetStatusEvent.NET_STATUS,doSO);
     } 

     public function onBWDone():void
     {
        }       

  private function doSO (e:NetStatusEvent):void
  {
       good=e.info.code == "NetConnection.Connect.Success";
       if (good)
       {
             //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" :
                       break;

                  case "change" :
                       textArea_chatDisplay.text += text_so.data.msg + "\n";
                       break;
             }
       }
  }

  private function cleanName (e:FocusEvent):void
  {
       textInput_chatName.text="";
  }

  private function sendMsg (e:MouseEvent):void
  {           
   noName=(textInput_chatName.text=="<Enter Name>" || textInput_chatName.text=="");
   if (noName)
   {
    textArea_chatDisplay.text += "Enter your name in the name field and then send. \n";
   }
   else
   {
       text_so.setProperty ("msg",textInput_chatName.text +": "+ textInput_chatEntry.text);
    textArea_chatDisplay.text += textInput_chatName.text +": "+ textInput_chatEntry.text + "\n";
       textInput_chatEntry.text = "";
   }
  }

  private function checkKey (e:FlexEvent):void
  {         
   noName=(textInput_chatName.text=="<Enter Name>" || textInput_chatName.text=="");
   if (noName)
   {
    textArea_chatDisplay.text += "You must enter your name \n";
   }
   else
   {
       text_so.setProperty ("msg",textInput_chatName.text +": "+ textInput_chatEntry.text);
    textArea_chatDisplay.text += textInput_chatName.text +": "+ textInput_chatEntry.text + "\n";
       textInput_chatEntry.text = ""; 
   }
  }

    ]]>
</mx:Script> 


<mx:Panel id="panel_chat"
 width="400" height="300"  
 title="Chat" x="0" y="0" cornerRadius="0">   

 <mx:TextArea id="textArea_chatDisplay" 
  width="100%" height="100%"
  valueCommit="textArea_chatDisplay.verticalScrollPosition=textArea_chatDisplay.maxVerticalScrollPosition"/>

 <mx:ControlBar id="controlBar_chat"
  direction = "vertical" 
  width="100%">

  <mx:Canvas width="100%">
            <mx:TextInput id="textInput_chatEntry"
             enter="checkKey(event)" right="60" left="0"/>
            <mx:Button id="button_chatSend" 
             label="Send" right="0"
             click="sendMsg(event)"/>    
  </mx:Canvas>

  <mx:Canvas width="100%">
   <mx:Label text="To:" 
    right="200"/>      
            <mx:ComboBox id="comboBox_chatSendTo" 
             right="60" 
             width="140">
    <mx:ArrayCollection>
       <mx:String>Everyone</mx:String>
    </mx:ArrayCollection> 
            </mx:ComboBox>  

            <mx:Button id="button_chatOptions"
             label="..." 
             right="0" width="52"/>    
  </mx:Canvas>

  <mx:Canvas width="100%">
   <mx:Label text="Name:" 
    right="200"/>    
            <mx:TextInput id="textInput_chatName"
             right="60" width="138"/>    
  </mx:Canvas>

    </mx:ControlBar>      

</mx:Panel>