flex+fms ShareObject简单在线聊天

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;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注