利用ShareObject实现简单聊天程序

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initFun()" width="507" height="434">
<mx:Script>
<![CDATA[
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	private var nc:NetConnection;
	private var clientID:Number;
	private var soChat:SharedObject;
	private var arr:Array = new Array();
	
	[Bindable]
	private var con:ArrayCollection;

	private function initFun():void
	{
		nc = new NetConnection();
		nc.objectEncoding = flash.net.ObjectEncoding.AMF0;
		nc.addEventListener(NetStatusEvent.NET_STATUS,statusHandler);
		nc.connect( "rtmp://localhost/SOSample" );
		//监听键盘回车键
		application.addEventListener(KeyboardEvent.KEY_DOWN,sendKeyHandler);
	}

	private function statusHandler(event:NetStatusEvent):void
	{
		if(event.info.code == "NetConnection.Connect.Success"){
			connectToChat();
		}
	}

	private function connectToChat():void
	{
		soChat = SharedObject.getRemote( "wxwred5", nc.uri, true );
		soChat.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );
		soChat.addEventListener( AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler );
		soChat.addEventListener( SyncEvent.SYNC, sharedObjectSyncHandler );
		soChat.client = this;
		soChat.connect( nc );
		
		soChat.send( "getName" );
	}

	public function getName():void
	{
		var msg:String=lTrim(rTrim(txtUser.text.valueOf()));
		trace( "getName: " + txtUser.text );
		if(msg.length==0){
			Alert.show("禁止刷屏");
			txtUser.text="";
		}else{
			soChat.setProperty("key",txtUser.text);
			txtUser.text="";		
		}

	}

	public function newName(str:String):void
	{
		arr.push(str);
		soChat.setProperty("arr",arr);
		con = new ArrayCollection(arr);
	}

	private function sharedObjectSyncHandler( event:SyncEvent ):void
	{
		trace( "sharedObjectSyncHandler:code: " + event.changeList );
		arr.push(event.target.data.key);
		con = new ArrayCollection(arr);
	}

	private function netStatusHandler( event:NetStatusEvent ):void
	{
		trace( "netStatusHandler:code: " + event.info.code );
	}
	private function asyncErrorHandler( event:AsyncErrorEvent ):void
	{
		trace( "asyncErrorHandler:code: " + event.error );
	}
	private function sendKeyHandler(event:KeyboardEvent):void
	{
		//Enter 键
		if(event.keyCode==13){
			getName();
		}
		//监听组合键 Ctrl+Enter 键
		if(event.keyCode==13&&event.ctrlKey){
			Alert.show("监听组合键 Ctrl+Enter 键");
			
		}
		
	}
	//去掉左空格
	public function lTrim(str:String):String {
		var regx:RegExp = /(^\s*)/g;
		return str.substr(regx.exec(str)[0].toString().length);
	}
	//取得右空格
	public function rTrim(str:String):String {
		var regx:RegExp = /(\s*$)/g;
		return str.substring(0, str.length - regx.exec(str)[0].toString().length);
	}
]]>
</mx:Script>
	<mx:Panel width="378" height="315" layout="absolute" y="50" x="70" title="聊天" fontSize="12">
		<mx:TextInput id="txtUser"  x="37" y="223" fontSize="12"/>
		<mx:Button click="getName()" label="发送"  x="220" y="223" fontSize="12"/>
		<mx:List id="listView" height="203" dataProvider="{con}" width="237" x="37" y="10"  fontFamily="Arial" fontSize="12"    verticalScrollPolicy="on"></mx:List>
	</mx:Panel>
</mx:Application>
 

 

你可能感兴趣的:(xml,.net,Flash,Adobe)