别人的flex聊天室

  1. package com.renaun.samples.net   
  2. {   
  3. import flash.net.NetConnection;   
  4. import flash.net.SharedObject;   
  5. import flash.events.NetStatusEvent;   
  6. import flash.events.SecurityErrorEvent;   
  7. import flash.events.AsyncErrorEvent;   
  8. import flash.events.IOErrorEvent   
  9. import flash.events.Event;   
  10. import flash.events.IEventDispatcher;   
  11. import mx.logging.LogLogger;   
  12.   
  13. [Event(name="success", type="flash.events.Event")]   
  14. [Event(name="failed", type="flash.events.Event")]   
  15. /**   
  16.  *  Note: This class was dynamic in ActionScript 2.0 but is now sealed.    
  17.  *  To write callback methods for this class, you can either extend the    
  18.  *  class and define the callback methods in your subclass, or you can    
  19.  *  use the client  property to refer to an object and define the callback    
  20.  *  methods on that object.   
  21.  */   
  22. dynamic public class FMSConnection extends NetConnection implements IEventDispatcher   
  23. {   
  24.   
  25.     //--------------------------------------------------------------------------   
  26.     //   
  27.     //  Constructor   
  28.     //   
  29.     //--------------------------------------------------------------------------   
  30.        
  31.     /**   
  32.      *  Constructor   
  33.      */   
  34.     public function FMSConnection()    
  35.     {   
  36.         super();   
  37.   
  38.     }   
  39.        
  40.     public var clientID:Number;   
  41.        
  42.     //--------------------------------------------------------------------------   
  43.     //   
  44.     //  Methods   
  45.     //   
  46.     //--------------------------------------------------------------------------   
  47.   
  48.     /**   
  49.      *  Connect   
  50.      */   
  51.     override public function connect( url:String, ...args ):void   
  52.     {   
  53.         // Set object encoding to be compatible with Flash Media Server   
  54.         this.objectEncoding = flash.net.ObjectEncoding.AMF0;   
  55.         NetConnection.defaultObjectEncoding   
  56.            
  57.         // Add status/security listeners   
  58.         this.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );   
  59.         this.addEventListener( SecurityErrorEvent.SECURITY_ERROR, netSecurityError );   
  60.         this.addEventListener( AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler );   
  61.         this.addEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler );        
  62.            
  63.         // TODO does not pass ...args into the super function   
  64.         super.connect( url );   
  65.     }   
  66.   
  67.   
  68.     /**   
  69.      *  setID   
  70.      */   
  71.     public function setId( id:Number ):*   
  72.     {   
  73. //LogLogger.debug( "FMSConnection::setId: id=" + id,"s");   
  74.   
  75.         if( isNaN( id ) ) return;   
  76.         clientID = id;   
  77.         return "Okay";   
  78.     }   
  79.   
  80.     /**   
  81.      *  Status Handler for the NetConnection class   
  82.      */   
  83.     private function netStatusHandler( event:NetStatusEvent ):void   
  84.     {   
  85.         switch( event.info.code ) {   
  86.             case "NetConnection.Connect.Success":   
  87. //Logger.debug( "FMSConnection:netStatusHandler:Success: connected: " + this.connected );   
  88.                 dispatchEvent( new Event( "success" ) );   
  89.             break;   
  90.             case "NetConnection.Connect.Failed":   
  91. //Logger.debug( "FMSConnection:netStatusHandler:Failed: connected: " + this.connected + " - " + event.info.code );   
  92.                 dispatchEvent( new Event( "failed" ) );   
  93.             break;             
  94.             default:   
  95. //Logger.debug( "FMSConnection:netStatusHandler:code: " + event.info.code );   
  96.             break;   
  97.         }   
  98.     }   
  99.   
  100.     private function netSecurityError( event:SecurityErrorEvent ):void {   
  101. //Logger.error( "FMSConnection:netSecurityError: " + event );   
  102.     }          
  103.   
  104.     private function asyncErrorHandler( event:AsyncErrorEvent ):void {   
  105. //Logger.error( "FMSConnection:asyncErrorHandler: " + event.type + " - " + event.error );   
  106.     }      
  107.   
  108.     private function ioErrorHandler( event:IOErrorEvent ):void {   
  109. //Logger.error( "FMSConnection:asyncErrorHandler: " + event.type + " - " + event.text );   
  110.     }      
  111.   
  112. }   
  113. }  

 

Flex代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"    
  3.     layout="vertical" horizontalAlign="center" verticalAlign="middle">   
  4.     <mx:Style>   
  5.         Application {   
  6.             background-image: "";   
  7.             background-color: #336699;     
  8.         }   
  9.         Panel, TabNavigator {   
  10.             padding-bottom:10;   
  11.             padding-left:10;   
  12.             padding-right:10;   
  13.             padding-top:10;    
  14.         }   
  15.         .chatBox {   
  16.             background-color: #EEEEEE;   
  17.         }   
  18.     </mx:Style>   
  19.     <mx:Script>   
  20.         <![CDATA[   
  21.             import mx.collections.ArrayCollection;   
  22.             import mx.controls.Text;   
  23.             import com.renaun.samples.net.FMSConnection;   
  24.             import mx.controls.Alert;   
  25.             import flash.net.SharedObject;   
  26.                
  27.             [Bindable]   
  28.             private var nc:FMSConnection;   
  29.             [Bindable]   
  30.             private var btnText:String = "Login";   
  31.                
  32.             [Bindable]   
  33.             private var dpUsers:ArrayCollection   
  34.             private var chatUsers:Array;   
  35.                
  36.             private var clientID:Number;   
  37.                
  38.             private var soChat:SharedObject;   
  39.                
  40.             private function connectUser():void   
  41.             {   
  42.                 NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;   
  43.                 SharedObject.defaultObjectEncoding  = flash.net.ObjectEncoding.AMF0;                   
  44.                    
  45.                 if( txtUser.text != "" && btnText == "Login" ) {   
  46.                     nc = new FMSConnection();   
  47.                     nc.addEventListener( "success", connectionSuccessHandler );   
  48.                     // works with nc.connect( "rtmp://localhost/SOSample" ); also   
  49.                     nc.connect( "rtmp://192.168.200.14/red" );   
  50.                 } else {   
  51.                     if( btnText == "Logout" ) {   
  52.                         // Close Connections   
  53.                         soChat.close();   
  54.                         nc.close();   
  55.                         btnText = "Login";   
  56.                     } else {   
  57.                         Alert.show( "Invalid Name!" );   
  58.                     }   
  59.                 }   
  60.                 keyEvent();   
  61.                    
  62.             }   
  63.                
  64.             private function connectionSuccessHandler( event:Event ):void   
  65.             {   
  66.                 btnText = "Logout";   
  67.   
  68.                 // Get Server Client ID   
  69.                 clientID = nc.clientID;   
  70.                    
  71.                 // Make SO and other Connection calls   
  72.                 connectToChat();   
  73.             }   
  74.                
  75.             private function connectToChat():void   
  76.             {   
  77.                 soChat = SharedObject.getRemote( "videoConferenceChat", nc.uri, true );   
  78.                 soChat.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );   
  79.                 soChat.addEventListener( AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler );   
  80.                 soChat.addEventListener( SyncEvent.SYNC, sharedObjectSyncHandler );   
  81.                 soChat.client = this;   
  82.                 soChat.connect( nc );   
  83. //              trace("helloWorld");   
  84.                 // Get the names for all the connected users   
  85.                 soChat.send( "getName" );   
  86.             }   
  87.   
  88.             public function sendMessage( msg:String ):void   
  89.             {   
  90.                 soChat.send( "newMessage",     
  91.                         "<font color=/"#" + cmpColorPicker.selectedColor.toString(16) + "/"><b>" + txtUser.text + "</b></font> - " + msg );   
  92.   
  93.                 txtMsg.htmlText = "";   
  94.             }   
  95.   
  96.             public function newMessage( msg:String ):void   
  97.             {   
  98. //Logger.info( "newMessage: " + msg );                 
  99.                 txtChatBox.htmlText += msg + "/n";   
  100.             }   
  101.             public function newName( name:String, chatID:Number ):void   
  102.             {   
  103. //Logger.info( "newName: name: " + name + " chatID: " + chatID );   
  104.                 // initialize   
  105.                 if( chatUsers == null )   
  106.                     chatUsers = new Array();   
  107.                    
  108.                 // Only add new users to array   
  109.                 var isNew:Boolean = true;   
  110.                 for( var i:int = 0; i < chatUsers.length;i++ ) {   
  111.                     if( chatUsers[ i ].data == chatID ) {   
  112.                         isNew = false;   
  113.                     }   
  114.                 }   
  115.                 if( isNew )   
  116.                     chatUsers.push( { label: name, data: chatID } );   
  117.                    
  118.                 // Sort Users   
  119.                 chatUsers.sortOn( "label", Array.CASEINSENSITIVE );   
  120.                    
  121.                 dpUsers = new ArrayCollection( chatUsers );    
  122.                    
  123.             }              
  124.             public function getName():void   
  125.             {   
  126. //Logger.info( "getName: " + txtUser.text );   
  127.                 // Clear out User array   
  128.                 chatUsers = new Array();   
  129.                 // Getting Users name is basically forcing each connect user to re-send their name   
  130.                 soChat.send( "newName", txtUser.text, clientID );   
  131.             }                      
  132.   
  133.     private function sharedObjectSyncHandler( event:SyncEvent ):void   
  134.     {   
  135.         trace(event.toString());   
  136. //      Logger.debug( "sharedObjectSyncHandler:code: " + event.changeList );   
  137.     }   
  138.   
  139.     private function netStatusHandler( event:NetStatusEvent ):void   
  140.     {   
  141.         trace(event.info);   
  142. //      Logger.debug( "netStatusHandler:code: " + event.info.code );   
  143.     }   
  144.     private function asyncErrorHandler( event:AsyncErrorEvent ):void   
  145.     {   
  146.         trace(event.toString());   
  147. //      Logger.debug( "asyncErrorHandler:code: " + event.error );   
  148.     }              
  149.         ]]>   
  150.     </mx:Script>   
  151.     <mx:Panel   
  152.         width="90%" height="90%"  
  153.         title="Basic Chat Application using Flex 3 and Red5"    
  154.         layout="vertical">   
  155.            
  156.         <mx:HBox width="100%">   
  157.             <mx:Label text="Name:"/>   
  158.             <mx:TextInput id="txtUser" enabled="{ ( btnText == 'Login' ) }"/>   
  159.             <mx:ColorPicker id="cmpColorPicker" showTextField="false" selectedColor="0x000000"/>             
  160.             <mx:Button    
  161.                 id="btnLogin"  
  162.                 label="{ btnText }"  
  163.                 click="connectUser()"/>   
  164.         </mx:HBox>   
  165.            
  166.         <mx:HDividedBox   
  167.             width="100%" height="100%">   
  168.             <mx:VBox    
  169.                 width="25%" height="100%">   
  170.                 <mx:Label text="Chat Users:" />   
  171.                 <mx:List   
  172.                     id="lstUsers"  
  173.                     dataProvider="{ dpUsers }"    
  174.                     width="100%"  
  175.                     height="100%" />   
  176.             </mx:VBox>   
  177.                
  178.             <mx:VBox    
  179.                 width="75%" height="100%">   
  180.                 <mx:TabNavigator   
  181.                     width="100%" height="100%">   
  182.                     <mx:VBox label="Main Chat">   
  183.                         <mx:TextArea    
  184.                             fontSize="18"  
  185.                             id="txtChatBox"  
  186.                             styleName="chatBox"  
  187.                             editable="false"  
  188.                             width="100%" height="100%" fontWeight="bold" textAlign="left"/>   
  189.                     </mx:VBox>   
  190.                 </mx:TabNavigator>   
  191.                        
  192.                 <mx:HBox width="100%">   
  193.                     <mx:TextInput width="100%" id="txtMsg"/>   
  194.                     <mx:Button    
  195.                         id="btnMessage"  
  196.                         label="Send Message"  
  197.                         click="sendMessage( txtMsg.text )" />   
  198.                 </mx:HBox>   
  199.             </mx:VBox>   
  200.         </mx:HDividedBox>   
  201.            
  202.     </mx:Panel>   
  203.     <mx:Script>   
  204.         <![CDATA[   
  205.                
  206.             public function keyEvent(){   
  207.                 txtMsg.addEventListener(KeyboardEvent.KEY_DOWN,sendM)   
  208.             }   
  209.             public function sendM(e:KeyboardEvent){   
  210.                    
  211.                 if(e.keyCode==13){   
  212.                     sendMessage(txtMsg.text);   
  213.                 }   
  214.             }   
  215.         ]]>   
  216.     </mx:Script>   
  217. </mx:Application>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值