⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flexfcs.mxml

📁 FlexChat聊天室
💻 MXML
字号:
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="650" height="516" creationComplete="init();" backgroundColor="#e6e6e6">
	<mx:Script> 
	     <![CDATA[
	     		import mx.collections.ArrayCollection; 
				import flash.net.*;
				import flash.events.*;
				import flash.utils.*;
				import mx.controls.*;
				import mx.core.UIComponent;
	
	            // Current release of FMS only understands AMF0 so tell Flex to 
				// use AMF0 for all NetConnection, NetStream, and SharedObject objects.
				NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
				//NetStream.defaultObjectEncoding     = flash.net.ObjectEncoding.AMF0;
				SharedObject.defaultObjectEncoding  = flash.net.ObjectEncoding.AMF0;
				
				// echoResponder is used when nc.call("echo", echoResponder ...) is called.
				private var echoResponder:Responder = new Responder(echoResult, echoStatus);
				
				// SharedObject and NetConnection vars
				private var nc:NetConnection;
				public var ro:SharedObject;
	            
	            private function init():void 
	            { 
					writeln("Initializing application... in player: " + flash.system.Capabilities.version + "\n");
					// create new connection to FMS and add listeners
	            	nc = new NetConnection();            	
					nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
					nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, netSecurityError);
	            } 
	            
	           	/** 
		         * connect is called whenever the connectButton is pressed
		         * and decides what to do based on the current label of the button.
		         * NOTE: the rtmp address is in this function. Change it if you need to.
		         */
	            private function connect():void
	            {
		        	if(userName.text == ""){
		        		Alert.show("Please enter a user name.");
		        		return;
		        	}
		        	switch(connectButton.label){
		        		case "Connect":
		        			connectButton.label = "Wait";
		        			connectButton.enabled = false;
		        			nc.connect("rtmp://kurosawa.wpcareyonline.com/chat_test", userName.text);
		        			//nc.connect("rtmp://localhost/chat_test", userName.text);
		        			nc.client = this;
		        		break;
		        		case "Disconnect":
		        			connectButton.label = "Connect";
		        			connectButton.enabled = true;
		        			nc.close();
		        		break;
		        	}
	            }
	            
	            private function netSecurityError(event:SecurityErrorEvent):void {
		            writeln("netSecurityError: " + event);
		        }
	             
	            /** 
				 * This method could be named anything - even onStatus. I've named it
				 * netStatus as in Dave Simmons example. In the docs they use the
				 * name netStatusHandler. Instead of receiving an information object
				 * it is passed an event that contains the information object.
				 */
				private function netStatus(event:NetStatusEvent):void 
				{
					// Write out information about connection events:
		            writeln("netStatus: " + event);
		            var info:Object = event.info;
		            for(var p:String in info) {
		                writeln(p + " : " + info[p]);
		            }
		            writeln("");

					switch (info.code) 
					{
						case "NetConnection.Connect.Success" :
							connectButton.label = "Disconnect";
		            		connectButton.enabled = true;
		            		sendButton.enabled = true; 
		            		
		            		writeln("Connecting non-persistent Remote SharedObject...\n");
							ro = SharedObject.getRemote("ChatUsers", nc.uri);
							if(ro){
								ro.addEventListener(SyncEvent.SYNC, OnSync);
								ro.connect(nc);
								ro.client = this; // refers to the scope of application and public funtions
							}
							getServerTime(); // get local time
						   break;
						case "NetConnection.Connect.Closed" :
							connectButton.label = "Connect";
		            		connectButton.enabled = true;
		            		sendButton.enabled = false;  
						   break;
						case "NetConnection.Connect.Failed" :
						   break;
						case "NetConnection.Connect.Rejected" :
						   break;
						default :
						   //statements
						   break;
					}
				}
				
				private function OnSync(event:SyncEvent):void 
				{
					// Show the ChangeList:
					var info:Object;
					var currentIndex:Number;
					var currentNode:Object;
					var changeList:Array = event.changeList;
					var temp:Array = new Array();
					
					writeln("---- Shared Object Data -----");
					for(var p:String in ro.data){ 
						writeln("OnSync> RO: " + p + ": " + ro.data[p]);
						temp.push(ro.data[p]);
					}
					this.usersList.dataProvider = temp; //update list of users

					for (var i:Number=0; i < changeList.length; i++) 
					{
						info =  changeList[i];
						for (var k:String in info){
							writeln("OnSync> changeList[" + i + "]." + k + ": " + info[k]);
						}
					}
				}
			
				/** echoResult is called when the echoResponder gets a result from the nc.call("echoMessage"..) */
		        private function echoResult(msg:String):void{
		        	writeln("echoResult: " + msg + "\n");
		        	this.serverTime.text = msg;
		        }
		        
		        /** echoResult is called when the echoResponder gets a error after a nc.call("echoMessage"..) */
		        private function echoStatus(event:Event):void{
		        	writeln("echoStatus: " + event);
		        }
		        
				/** sendMessage is called when the sendButton is pressed to test ns.send */
				private function sendMessage():void{
					// call our remote function and send the message to all connected clients
					nc.call("msgFromClient", null, sendMessageInput.text);
					sendMessageInput.text = "";
				}
				
				/** get server tme  */
				private function getServerTime():void{
					nc.call("getServerTime", echoResponder,  sendMessageInput.text);
				}

				/** showMessage is the function called by the inStream. See the netStatus function */
				public function showMessage(msg:String):void{
					writeln("showMessage: " + msg + "\n");
				}
				
				public function setUserID(msg:Number):void{
					writeln("showMessage: " + msg + "\n");
				}
				
				public function setHistory(msg:String):void{
					writeln("showHistory: " + msg + "\n");
				}
				
				public function msgFromSrvr(msg:String):void{
					writeMessage(msg);
				}	
						
				/** 
				 * writeln writes text into the traceArea TextArea instead of using trace. 
				 * Note to get scrolling to the bottom of the TextArea to work validateNow()
				 * must be called before scrolling.
				 */		
				public function writeln(msg:String):void{
					traceArea.text += msg + "\n";
					traceArea.validateNow();
					traceArea.verticalScrollPosition = traceArea.maxVerticalScrollPosition;
				}
				
				/** 
				 * writeMessage writes text into the main chat text area
				 */		
				public function writeMessage(msg:String):void{
					messageArea.text += msg + "\n";
					messageArea.validateNow();
					messageArea.verticalScrollPosition = messageArea.maxVerticalScrollPosition;
				}
	     ]]> 
     </mx:Script> 
	<mx:List width="127" height="210" x="513" y="66" id="usersList"/>
	<mx:TextArea width="495" height="210" id="messageArea" wordWrap="true" x="10" y="66"/>
	<mx:TextInput text="hello" id="sendMessageInput"  width="366" x="77" y="284"/>
	<mx:Button label="Send" id="sendButton" enabled="false" click="sendMessage();" x="451" y="284"/>
	<mx:TextInput id="serverTime"  width="243" x="397" y="10"/>
	<mx:Button label="Connect" id="connectButton" click="connect();" x="212" y="10" width="90"/>
	<mx:TextInput x="10" y="10" width="194" id="userName"/>
	<mx:TextArea width="630" height="156" id="traceArea" wordWrap="true" x="10" y="350"/>
	<mx:Label x="10" y="324" text="Trace Window" fontWeight="bold"/>
	<mx:Label x="10" y="40" text="Message Window" fontWeight="bold"/>
	<mx:Label x="513" y="40" text="Users" fontWeight="bold"/>
	<mx:Label x="319" y="12" text="Local Time:"/>
	<mx:Label x="10" y="286" text="Message:"/>
</mx:Canvas>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -