Newer
Older
chatpod / Chat.mxml
@reddawg reddawg on 30 Nov 2007 5 KB chatpod
<?xml version="1.0" encoding="utf-8"?>
<!--
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2003-2006 Adobe Macromedia Software LLC and its licensors.
// All Rights Reserved.
// The following is Sample Code and is subject to all restrictions on such code
// as contained in the End User License Agreement accompanying this product.
// If you have received this file from a source other than Adobe,
// then your use, modification, or distribution of it requires
// the prior written permission of Adobe.
//
////////////////////////////////////////////////////////////////////////////////
-->
<mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*"
	horizontalAlign="center" verticalAlign="middle">

  <mx:Script>
  <![CDATA[
  	import mx.collections.XMLListCollection;

		import mx.messaging.messages.*;
		import mx.messaging.events.*;
      import mx.controls.Alert;		
		
		[Bindable]
		public var userId:String;
		
        [Bindable]
        private var colorCode:Number;
        [Bindable]
        public var usersList:XMLListCollection;
		
    public function initComp():void {
      usersList = new XMLListCollection();
	  consumer.subscribe();
	  }

    public function requestUsers():void {
	  var message:AsyncMessage;
	  
	  
	  message = new AsyncMessage();

      message.headers["cmd"] = 1;
      message.body = {
      	userId: "System Notification",
      	msg: "User " + userId + " Has Signed On",
      	color: colorCode
        };
      producer.send(message);	  
      message = new AsyncMessage();
	  
	  message.headers["cmd"] = 2;
	  message.body = {
		userId: userId
	    };
	  producer.send(message);
      }
    public function sendDepart():void {
      var message:AsyncMessage;
      
      message = new AsyncMessage();
      message.headers["cmd"] = 1;
      message.body = {
      	userId: "System Notification",
      	msg: "User " + userId + " Has Signed Off",
      	color: colorCode
        };
      producer.send(message);
      
      message = new AsyncMessage();
	  message.headers["cmd"] = 4;
	  message.body = {
		userId: userId
	    };
	  producer.send(message);	
	  
	  consumer.unsubscribe();
      } /* end sendDepart() */
      
		public function send():void
		{
			var message:AsyncMessage = new AsyncMessage();
			message.headers["cmd"] = 1;
			message.body = {
			    userId: userId,
			    msg: msg.text,
			    color: colorCode
			};
			producer.send(message);
			msg.text="";
		}
		
    public function messageHandler(event:MessageEvent):void {
	  var body:Object = event.message.body;
	  var user:String;
	  var i:Number;
      switch (Number(event.message.headers["cmd"])) {
        case 1:
		  if (log != null && body.userId != undefined) {
		    var txt:String = body.userId + ": " + body.msg + "\n";
		    if (body.color == undefined) {
              log.text += txt;
              }
            else {
    		  var colorStr:String = "#" + Number(body.color).toString(16);
              log.htmlText += "<font color=\"" + colorStr + "\">" + txt + "</font>";                    
              }
            //The TextArea needs to re-compute its size to update 
            //maxVerticalScrollPosition but is optimized to wait until the 
            //next drawing pass.  Therefore we call validateNow() to 
            //force the update before we try to update the scroll position
            log.validateNow();
            log.verticalScrollPosition=log.maxVerticalScrollPosition;
  		    }
   		  break;	
    	case 2:
    	  if (body.userId != userId) {
		    user = "<user userName=\"" + body.userId + "\" />";
		    usersList.addItem(new XML(user));
    	    }
		  var message:AsyncMessage = new AsyncMessage();	  
		  message.headers['cmd'] = 3;
		  message.body = {
			userId: userId,
			tuid: body.userId
			};
		  producer.send(message);    	
    	  break;
		case 3:
		  if (body.tuid == userId) {
		  	user = "<user userName=\"" + body.userId + "\" />";
		  	usersList.addItem(new XML(user));
		    }
		  break;
        case 4:
          for (i = 0;i < usersList.length;i++) {
          	if (body.userId == usersList[i].@userName) {
          	    usersList.removeItemAt(i);
          	    }
            }
          break;
          }
		}
		
  private function doReconnect():void {
    //Alert.show("FDS: Failed");
    consumer.subscribe();
    }
  ]]>
  </mx:Script>

	<mx:Producer id="producer" destination="podz_chat"/>
	<mx:Consumer id="consumer" destination="podz_chat" message="messageHandler(event)" channelDisconnect="doReconnect()" />

<mx:VBox width="100%" height="100%">
	

  			    <mx:Label text="Chat [{userId}]"/>
  			    <mx:HBox width="100%" height="100%">
				<mx:TextArea id="log" width="100%" height="100%" editable="false" />
				<mx:DataGrid width="100" id="onlineUsers" height="100%" dataProvider="{usersList}">
				<mx:columns>
					<mx:DataGridColumn headerText="User" dataField="@userName" />
				</mx:columns>
				</mx:DataGrid>
</mx:HBox>
				<mx:HBox width="100%" height="30" paddingTop="0" paddingBottom="0">
					<mx:TextInput id="msg" enter="send()" width="100%"/>
					<mx:Button label="Send" click="send()"/> 
					<mx:ColorPicker id="colorPicker" change="colorCode = colorPicker.selectedColor"/>
				</mx:HBox>
</mx:VBox>
</mx:Panel>