flex+fms ShareObject简单在线聊天

www.111cn.net 更新:2014-03-22 编辑:kp12345 来源:转载
下面我们一起来看一个关于flex+fms ShareObject简单在线聊天,希望此例子对各位同学会带来帮助哦。
 代码如下 复制代码

flex代码:

<?xml version="1.0" encoding="utf-8"?>  
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" backgroundColor="white" layout="vertical" initialize="BasicPracticeSO();">  
 <mx:Script>  
  <![CDATA[  
   private var nc:NetConnection;  
   private var shareObject:SharedObject;  
   private var monthDay:Date;  
   private var isConnectSuccess:Boolean;  
   private static const RTMP_URL:String="rtmp://localhost/chat";  
 
 
   private function BasicPracticeSO():void{  
    nc=new NetConnection;  
    nc.connect(RTMP_URL);  
    nc.addEventListener(NetStatusEvent.NET_STATUS,operateShareObject);  
   }  
 
   private function operateShareObject(evt:NetStatusEvent):void{  
    isConnectSuccess=(evt.info.code=="NetConnection.Connect.Success");  
    if(isConnectSuccess){  
     shareObject=SharedObject.getRemote("userInfo",nc.uri,false);  
     monthDay=new Date();  
     shareObject.connect(nc);  
     shareObject.addEventListener(SyncEvent.SYNC,detectSO);  
    }  
   }  
 
   private function detectSO(e:SyncEvent):void{  
    for(var chng:uint;chng<e.changeList.length;chng++){  
     switch(e.changeList[chng].code){  
      case "change":  
       updateAllClientMsg();  
       break;  
      case "clear":  
       break;  
      case "success":  
       break;  
      default:  
       break;  
     }  
    }  
   }  
 
   private function updateAllClientMsg():void{  
    soList.text+=shareObject.data.msg+"n";  
   }  
 
 
   private function addDataToShareObject():void{  
    shareObject.setProperty("msg",msgText.text);  
    soList.text+=msgText.text+"n";  
    msgText.text="";  
   }  
  ]]>  
 </mx:Script>  
 <mx:Label text="ShareObject data:"/>  
 <mx:TextArea id="soList" width="300" height="120">  
 
 </mx:TextArea>  
 <mx:Form>  
  <mx:FormHeading label="add data to ShareObject:"/>  
  <mx:FormItem label="msg:">  
   <mx:TextInput id="msgText"/>  
  </mx:FormItem>  
  <mx:FormItem>  
   <mx:Button label="send Msg" click="addDataToShareObject();"/>  
  </mx:FormItem>  
 </mx:Form>  
</mx:Application>

FMS 代码:

Client.prototype.echo = function(msg){
 trace("client.echo: " + msg);
 return msg;
}

相关内容