chat.as

来自「flex实现的前台」· AS 代码 · 共 100 行

AS
100
字号
package cn.humanmonth.chat{
	import cn.humanmonth.chat.daemon.KeepAliveDaemon;
	import cn.humanmonth.chat.popup.NickName;
	
	import flash.display.DisplayObject;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.media.Sound;
	
	import mx.core.Application;
	import mx.managers.PopUpManager;
	/**
	 * 
	 * @author Piterlin
	 * Chat为主类,协调各功能块的合作
	 */
	public class Chat {
		private var connector:Connector;
		private var protocol:Protocol;
		private var actions:Array;
		private var face:Face;
		private var keepAliveDaemon:KeepAliveDaemon;
		[Embed(source="/music/msg.mp3")]
		[Bindable]
		private var MsgSound:Class;
		private var msgSound:Sound=new MsgSound() as Sound;
		public function Chat(face:Face) {
			connector=new Connector(initConnectEvent,onDataEvent,socketIOErrorEvent,httpIOErrorEvent);
			protocol=new Protocol(connector);
			this.face=face;
			face.addChangeNameListener(changeNameEvent);
			face.getMyIn().addEventListener(KeyboardEvent.KEY_DOWN,onInputEnter);
			this.actions=new ActionRegister(face).getActions();
			this.face.getSendButton().addEventListener(MouseEvent.CLICK,sendButtonEvent);
		}
		
		private function changeNameEvent(event:Event):void{
	    var nickName:NickName=NickName(PopUpManager.createPopUp(DisplayObject(Application.application),NickName,true));
	    PopUpManager.centerPopUp(nickName);
	    nickName.setMsgArea(face.getMsgArea());
	    nickName.setProtocol(this.protocol);
	    
		}
		
		public function httpIOErrorEvent(event:IOErrorEvent):void{
		    face.getMsgArea().htmlText+="(系统消息):服务端链不上,请稍后再试<br>";
		}
		
		/**
		 *  用于处理Socket的IO异常
		 */
		public function socketIOErrorEvent(event:IOErrorEvent):void {
		//	Alert.show('进入处理');
			face.getMsgArea().htmlText+="(系统消息):连接已关闭,正在尝试连接服务端```<br>";
			connector.connect();
		}
		/**
		 * socket链接后做的第一个件事
		 */
		public function initConnectEvent(event:Event):void {
			protocol.getAllUserList();
			keepAliveDaemon=new KeepAliveDaemon(this.protocol,this.connector);
		}

        /**
         * 处理“发送”按钮的发送事件 
         */
        public function sendButtonEvent(event:Event):void{
        this.protocol.sendMsg(this.face.getMyIn().text)
        face.getMyIn().text="";
        }
		public function onDataEvent(event:DataEvent):void {
			var text:String=event.data;
			msgSound.play();
		//	Alert.show(text);
			for each (var action:Action in actions) {
				if (action.candeal(text)) {
					action.deal(text);
					return;
				}
			}
			this.connector.setShouldSendKeepAliveFalse();
		}
		/**
		 * 当用户对输入框按回车时,触发发送事件 
		 */
		public function onInputEnter(event:KeyboardEvent):void {
			if (event.keyCode==13) {
				protocol.sendMsg(face.getMyIn().text);
			face.getMyIn().text="";
			}
		}
		public function getProtocol():Protocol {
			return this.protocol;
		}
	}
}

⌨️ 快捷键说明

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