自己稍微修改下的聊天工具:
package
{
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.net.NetConnection;
import flash.net.NetGroup;
import flash.net.GroupSpecifier;
import flash.events.NetStatusEvent;
import flash.events.MouseEvent;
import flash.text.TextFormat;
import flash.events.KeyboardEvent;
public class Main extends Sprite
{
private var px;
private var py;
private var loginPanel;
private var chatPanel;
private var nc:NetConnection;
private var group:NetGroup;
private var userName:String;
private var connected:Boolean = false;
private var keyObj;
private var right:Boolean = true;
public function Main():void
{
stage.scaleMode=StageScaleMode.NO_SCALE;
Init();
}
private function Init()
{
px = stage.stageWidth;
py = stage.stageHeight;
keyObj = {};
login();
}
private function login()
{
loginPanel=new LoginPanel();
loginPanel.x = px / 2 - loginPanel.width / 2;
loginPanel.y = py / 2 - loginPanel.height / 2;
loginPanel.submit.label = "登陆";
loginPanel.submit.addEventListener(MouseEvent.CLICK,loginHandler);
addChild(loginPanel);
}
private function loginHandler(e:MouseEvent)
{
if (loginPanel.nicknameTxt.text == "" || loginPanel.nicknameTxt.length <= 1)
{
echo("请输入昵称");
return;
}
else if (loginPanel.passwordTxt.text=="")
{
echo("请输入密码");
return;
}
else if (loginPanel.passwordTxt.length<6)
{
echo("你的密码少于六位");
return;
}
userName = loginPanel.nicknameTxt.text;
echo("欢迎你参加!");
loginPanel.submit.removeEventListener(MouseEvent.CLICK,loginHandler);
removeChild(loginPanel);
showPanel();
}
private function showPanel()
{
chatPanel=new ChatPanel();
chatPanel.x = px / 2 - chatPanel.width / 2;
chatPanel.y = py / 2 - chatPanel.bg.height / 2;
trace(chatPanel.width+"*"+chatPanel.height);
var txtFormat=new TextFormat();
txtFormat.size = 13;
chatPanel.outTxt.editable = false;
chatPanel.inTxt.setStyle("textFormat",txtFormat);
chatPanel.outTxt.setStyle("textFormat",txtFormat);
chatPanel.sendBtn.label = "发送";
chatPanel.sendBtn.width=72;
//chatPanel.mesTxt.text = userName;
chatPanel.sendBtn.addEventListener(MouseEvent.CLICK,clickHandler);
trace(stage);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
addChild(chatPanel);
thisTime();
chatPanel.stage.focus = chatPanel.inTxt.textField;
LinkFms();
}
private function keyDownHandler(e:KeyboardEvent)
{
keyObj[e.keyCode] = true;
if (keyObj[17] && keyObj[13])
{
chatPanel.sendBtn.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
chatPanel.inTxt.text = "";
}
}
private function keyUpHandler(e:KeyboardEvent)
{
keyObj[e.keyCode] = false;
}
private function clickHandler(e:MouseEvent)
{
if (chatPanel.inTxt.text != '' && chatPanel.inTxt.text.length > 1)
{
sendMessage(chatPanel.inTxt.text);
chatPanel.inTxt.text = "";
chatPanel.stage.focus = chatPanel.inTxt.textField;
chatPanel.inTxt.setSelection(0,0);
}
}
private function LinkFms()
{
nc=new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
nc.connect("rtmfp:");
}
private function netStatusHandler(e:NetStatusEvent)
{
switch (e.info.code)
{
case "NetConnection.Connect.Success" :
//txtUser.text = userName;
//chatPanel.mesTxt.text = userName;
//连接成功,创建需要广播的群组
setupGroup();
break;
case "NetGroup.Connect.Success" :
connected = true;
break;
case "NetGroup.Posting.Notify" :
//当获取到群组post群发通知的时候调用相关函数
receiveMessage(e.info.message);
break;
case "NetGroup.Neighbor.Connect" :
case "NetGroup.Neighbor.Disconnect" :
/*for(var i in event.info)
{
trace([i,event.info[i]])
}*/
break;
}
}
private function sendMessage(txt:String):void
{
//注意,post方法传递的是一个对象Object,由此大家可以方便的在此对象中添加各种需要传递的属性:)
var message:Object = new Object();
message.text = txt;
//转换一个peerID的群组地址,以适合使用sendToNearest()方法。
message.sender = group.convertPeerIDToGroupAddress(nc.nearID);
message.userName = userName;
group.post(message);
receiveMessage(message);
}
private function setupGroup():void
{
/*
GroupSpecifier类可以构造一个传递到NetStream和NetGroup的构造函数的加密groupspec字符串。
一个groupspec指定一个RTMFP P2P群组,包括功能、限制和成员授权等信息。
构造函数中的"myGroup/groupOne"为群组标识,可以随意字符串代替
*/
var groupspec:GroupSpecifier = new GroupSpecifier("myGroup/groupOne");
//设置群组中post群发方式是否可用
groupspec.postingEnabled = true;
//指定是否能通过ip多播方式交换更新关于组成员的信息。
groupspec.ipMulticastMemberUpdatesEnabled = true;
/*
将相关的群组加入某个IP地址并侦听相关的端口
Multicast的IP地址需要从不低于224开始,端口需高于1024 ————“224.0.0.0:1024”
为保证唯一性,一般设置高一些比较可靠:)
*/
groupspec.addIPMulticastAddress("225.225.0.1:30303");
//在已成功的连接nc上创建一个群组;
//groupspecWithAuthorizations:返回一个包含授权的加密的groupspec字符串, 传给NetGroup的构造函数。
group = new NetGroup(nc,groupspec.groupspecWithAuthorizations());
//为群组增加事件侦听
group.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
}
public function receiveMessage(message:Object):void
{
var txt="<font color='#ff0000'><b>" + message.userName + ":" + "</b></font> "+message.text;
writeText(txt);
//writeText(message.userName+": "+message.text);
}
private function writeText(str:String):void
{
chatPanel.outTxt.condenseWhite = true;
var isTime = thisTime();
chatPanel.outTxt.htmlText += "<font color='#ff0000'> 【" + isTime + " 】</font> " + str;
}
private function echo(str)
{
trace(str);
}
private function thisTime()
{
var myDate:Date=new Date();
var myTime:Array=new Array();
myTime[0] = myDate.fullYear;
myTime[1] = myDate.month + 1;
myTime[2] = myDate.date;
myTime[3] = myDate.hours;
myTime[4] = myDate.minutes;
myTime[5] = myDate.seconds;
if (right)
{
var txt:String;
right = false;
if (myTime[3] > 0 && myTime[3] < 6)
{
txt = "清晨好,欢迎光临:";
}
else if (myTime[3]>=6&&myTime[3]<12)
{
txt = "上午好,欢迎光临:";
}
else if (myTime[3]>=12&&myTime[3]<18)
{
txt = "下午好,欢迎光临:";
}
else if (myTime[3]>=18&&myTime[3]<=24)
{
txt = "晚上好,欢迎光临:";
}
var crazy= "<font color='#ff0000'><b>" +myTime[0]+"年"+myTime[1]+"月"+myTime[2]+"日 "+txt+ userName+ "</b></font> ";
chatPanel.mesTxt.htmlText = crazy;
}
for (var i:uint=1; i<=5; i++)
{
if (myTime[i] < 10)
{
myTime[i] = String(0 + String(myTime[i]));
}
}
var isTime = myTime[3] + ":" + myTime[4] + ":" + myTime[5];
return isTime;
}
}
}