📄 roomadmthread.java
字号:
package chat;
import chat.*;
import java.util.*;
import java.net.*;
import java.awt.*;
public class RoomAdmThread extends Thread {
InetAddress clientIP; //客户IP地址
int clientport; //客户端口
int type; //服务类型
DatagramPacket sendpacket;//声明发送和接收数据包
DatagramPacket receivepacket;//声明发送和接收数据包
DatagramSocket sendsocket; //声明发送和接收DatagramSocket
public RoomAdmThread() {
}
//构造函数(传入参数:接收的数据报和对应的数据报类型)
public RoomAdmThread(DatagramPacket packet,int ty) {
receivepacket=packet;
clientport=receivepacket.getPort();
clientIP=receivepacket.getAddress();
type=ty;
}
//向客户端发送所有房间信息
public void sendRoomInfo()
{
String pac=new String();
pac="23";
for(int i=0;i<WholeChatRoomInfo.rooms.size();i++)
{
RoomInfo rInfo=new RoomInfo();
rInfo=(RoomInfo)WholeChatRoomInfo.rooms.elementAt(i);
pac+=rInfo.ToRoomNetpac();
}
sendUdp(pac);
}
//发送的所给num的房间消息
public void sendRoomMessage(int num)
{
String pac;
RoomInfo rInfo=(RoomInfo)WholeChatRoomInfo.rooms.elementAt(num);
for(int i=0;i<rInfo.str.size();i++)
{
sendUdp((String)rInfo.str.elementAt(i));
}
}
//处理发送用户进入某房间信息文本
public void patchgotoInfo(String name,int num)
{
//发送着+方式+接收者+内容+颜色
//String color="12132";
String pac="27@-1@"+name;
RoomInfo rInfo = (RoomInfo)WholeChatRoomInfo.rooms.elementAt(num);
for(int i=0;i<rInfo.user.size();i++)
{
UserTempInfo temp=new UserTempInfo();
temp=(UserTempInfo)rInfo.user.elementAt(i);
MessagePatchThread mesPatch=new MessagePatchThread(temp,pac);
mesPatch.start();
}
}
//处理用户退出某房间信息文本
public void patchgooutInfo(String name,int num,int type)
{
String pac="27@"+Integer.toString(type)+"@"+name;
RoomInfo rInfo = (RoomInfo)WholeChatRoomInfo.rooms.elementAt(num);
if(type!=-3)
sendUdp(pac);
for(int i=0;i<rInfo.user.size();i++)
{
ChatFrame.textArea1.append(pac);
UserTempInfo temp=new UserTempInfo();
temp=(UserTempInfo)rInfo.user.elementAt(i);
MessagePatchThread mesPatch=new MessagePatchThread(temp,pac);
mesPatch.start();
}
}
//用户进入房间处理函数
public boolean gointoRoom(String name,int num)
{
int type=1;
String pac1="24@";
if(WholeChatRoomInfo.findtickname(name,num)) //被踢
{
pac1+="0@1@"+Integer.toString(WholeChatRoomInfo.reintime);
sendUdp(pac1);
return false;
}
for(int i=0;i<WholeChatRoomInfo.wander.size();i++)
{
UserTempInfo info=(UserTempInfo)WholeChatRoomInfo.wander.elementAt(i);
if(name.compareTo(info.getname())==0)
{
String pac2="24@";
RoomInfo rInfo = (RoomInfo)WholeChatRoomInfo.rooms.elementAt(num);
if(rInfo.max_num<rInfo.user.size()) //人员满
{
pac1+="0@4"; //人员满
sendUdp(pac1);
return false;
}
int n =rInfo.FindMaxSeq()+1;
pac2+="1@";
int isAdm=rInfo.user.size();
if(isAdm==0)
{
rInfo.adm_name = name;
pac2 += "1"; //第一个为管理员
}
else
{
pac2+="0";
}
info.userroomnum=num;
info.sequency=n;
//发送消息
ChatFrame.textArea1.append(pac2);
if(rInfo.AddTalker(info))
{
if(sendUdp(pac2))
{
WholeChatRoomInfo.rooms.setElementAt(rInfo, num);
WholeChatRoomInfo.wander.remove(i);
return true;
}
}
else
{
pac1+="0@3"; //已经登陆
sendUdp(pac1);
return false;
}
}
}
pac1+="0@2"; //还未登陆
sendUdp(pac1);
return false;
}
//退出房间处理函数
public boolean getoutofRoom(String name,int num)
{
int type=1;
RoomInfo rInfo=(RoomInfo)WholeChatRoomInfo.rooms.elementAt(num);
if(rInfo.DealTalker(name))
{
return true;
}
else
return false;
}
//UDP发送函数
public boolean sendUdp(String pac)
{
byte Array[]=new byte[255];
Array=pac.getBytes();
sendpacket=new DatagramPacket(Array,Array.length,
clientIP,
clientport);
try{
sendsocket=new DatagramSocket();
sendsocket.send(sendpacket);
return true;
}
catch(Exception e)
{
return false;
}
}
//发送消息处理函数
public void patchmessageInfo(String mes)
{
try{
//接收客户端来的数据
//接收者 发送者姓名 方式 是否私聊 是否过滤 发送内容 颜色
String receiver=new String();
String sender=new String();
String expression=new String();
String secret=new String();
String guolv=new String();
String str=new String();
String color=new String();
char mark = '@';
int index = mes.indexOf(mark);
receiver=mes.substring(0,index);
mes=mes.substring(index+1);
index = mes.indexOf(mark);
sender=mes.substring(0,index);
mes=mes.substring(index+1);
index = mes.indexOf(mark);
expression=mes.substring(0,index);
mes=mes.substring(index+1);
index = mes.indexOf(mark);
secret=mes.substring(0,index);
mes=mes.substring(index+1);
index = mes.indexOf(mark);
guolv=mes.substring(0,index);
mes=mes.substring(index+1);
index = mes.indexOf(mark);
str=mes.substring(0,index);
mes=mes.substring(index+1,mes.length());
color=mes;
//mes=mes.substring(index+1);
//发送着+标志+方式+接收者+内容+颜色
//根据要求发送到指定的用户
String pac="27@"+"0@"+receiver+"@"+expression+"@"+sender+"@"+str+"@"+color;
RoomInfo rInfo=new RoomInfo();
rInfo=(RoomInfo)WholeChatRoomInfo.rooms.elementAt(WholeChatRoomInfo.finduserRoom(sender));
//私聊
if(receiver.compareTo("大家")!=0 && secret.compareTo("1")==0)
{
for(int i=0;i<rInfo.user.size();i++)
{
UserTempInfo tempinfo = (UserTempInfo) rInfo.user.elementAt(i);
if(receiver.compareTo(tempinfo.name)==0)
//send
{
MessagePatchThread mesPatch=new MessagePatchThread(tempinfo,pac);
mesPatch.start();
return ;
}
}
}
//公聊
else
{
rInfo=(RoomInfo)WholeChatRoomInfo.rooms.elementAt(WholeChatRoomInfo.finduserRoom(sender));
rInfo.addmessage(pac);
WholeChatRoomInfo.rooms.setElementAt(rInfo,WholeChatRoomInfo.finduserRoom(sender));
for (int i = 0; i < rInfo.user.size(); i++) {
UserTempInfo tempinfo = (UserTempInfo) rInfo.user.elementAt(i);
//send
MessagePatchThread mesPatch=new MessagePatchThread(tempinfo,pac);
mesPatch.start();
}
return ;
}
}
catch(Exception e)
{
}
}
//从房间将某用户踢出处理函数
public boolean tickuser(String received)
{
int index=0;
char mark='@';
index = received.indexOf(mark, 0);
String name = received.substring(0, index );
received = received.substring(index+1);
//获取被踢人的姓名
index = received.indexOf(mark, 0);
String tickname = received.substring(0, index );
received = received.substring(index+1);
//获取被踢原因
index = received.indexOf(mark, 0);
String course = received.substring(0, index );
String pac="30@";
if(name.compareTo(tickname)==0)
{
pac+="0";
sendUdp(pac);
return false;
}
UserTempInfo uInfo=new UserTempInfo();
uInfo=WholeChatRoomInfo.getinfobyName(name);
int num=uInfo.userroomnum;
if(num>=0 )
{
RoomInfo rInfo = (RoomInfo) WholeChatRoomInfo.rooms.elementAt(num);
//是否为管理员
if(name.compareTo(rInfo.adm_name)==0)
{
WholeChatRoomInfo.wander.addElement(WholeChatRoomInfo.getinfobyName(
tickname));
patchgooutInfo(tickname, num,-3);
//设置tick后的情况
try{
Date current = new Date();
TickInfo tick= new TickInfo(tickname,rInfo.room_name,current);
WholeChatRoomInfo.ticks.add(tick);
}catch(Exception e)
{
}
rInfo.DealTalker(tickname);
WholeChatRoomInfo.rooms.setElementAt(rInfo, num);
pac+="1";
sendUdp(pac);
return true;
}
pac+="0";
sendUdp(pac);
return false;
}
else
{
pac+="0";
sendUdp(pac);
return false;
}
}
//退出房间
public void exitroom(String name)
{
int num=WholeChatRoomInfo.finduserRoom(name);
if(num>=0)
{
RoomInfo rInfo = (RoomInfo) WholeChatRoomInfo.rooms.elementAt(num);
WholeChatRoomInfo.wander.addElement(WholeChatRoomInfo.getinfobyName(name));
if(rInfo.DealTalker(name))
{
UserTempInfo uInfo = new UserTempInfo();
uInfo = WholeChatRoomInfo.getinfobyName(rInfo.adm_name);
String pac = "32@1";
MessagePatchThread mesPatch = new MessagePatchThread(uInfo, pac);
mesPatch.start();
}
WholeChatRoomInfo.rooms.setElementAt(rInfo, num);
//发送消息
patchgooutInfo(name, num,-2);
}
}
//类执行体
public void run()
{
try {
//接收的数据报字符串
String received = new String(receivepacket.getData());
char mark = '@';
int type = 0;
int index = received.indexOf(mark);
type = Integer.parseInt(received.substring(0, index));
received = received.substring(index + 1);
//分析数据报是何种类型
switch (type) {
case 3:
//发送房间消息
sendRoomInfo();
break;
case 4:
//进入房间
index = received.indexOf(mark, 0);
String name = received.substring(0, index);
//ChatFrame.textArea1.append(name);
int roomnum = 0;
received = received.substring(index + 1);
index=received.indexOf(mark,0);
roomnum = Integer.parseInt(received.substring(0, index));
//ChatFrame.textArea1.append(received.substring(0,index));
if (gointoRoom(name, roomnum)) {
patchgotoInfo(name, roomnum);
//UserTempInfo userinfo = new UserTempInfo();
//gei suo you 人都要发
//userinfo.IP = clientIP;
//userinfo.port = clientport;
PatchUserInfoThread rInfothread = new PatchUserInfoThread();
rInfothread.patchUserInfo();
sendRoomMessage(roomnum);
}
break;
case 5: //发送消息
patchmessageInfo(received);
break;
case 6: //发送文本列表
break;
case 7: //退出请求
index = received.indexOf(mark, 0);
name = received.substring(0, index);
exitroom(name);
PatchUserInfoThread rInfothread1 = new PatchUserInfoThread();
rInfothread1.patchUserInfo();
break;
case 9: //踢人包
tickuser(received);
PatchUserInfoThread rInfothread2 = new PatchUserInfoThread();
rInfothread2.patchUserInfo();
break;
case 11: //整个退出
index=received.indexOf(mark);
name=received.substring(0,index);
if(WholeChatRoomInfo.findName(name))
{
exitroom(name);
PatchUserInfoThread rInfothread3 = new PatchUserInfoThread();
rInfothread3.patchUserInfo();
}
ExitChatRoom exit=new ExitChatRoom(name);
exit.start();
break;
}
}//出错机制
catch (Exception e) {
ChatFrame.textArea1.append("Wrong!");
e.printStackTrace() ;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -