📄 serverreceivethread.java
字号:
import javax.swing.*;
import java.io.*;
import java.net.*;
public class ServerReceiveThread extends Thread{
JTextArea textarea;//区编辑框
JLabel jlabel;//标签,在本类中主要用于构造函数
JComboBox combobox;//组合框
Dan client;//定义一个Dan类的对象,以实现该类里面的功能
User userinfolist;//定义一个User类的对象
public boolean isstop;//定义一个布尔变量,用于程序的控制
public ServerReceiveThread(JTextArea textarea,JLabel jlabel,JComboBox combobox,Dan client,User userinfolist)
{//构造方法
this.textarea=textarea;
this.jlabel=jlabel;
this.client=client;
this.userinfolist=userinfolist;
this.combobox=combobox;
isstop=false;
}
public void run()//执行方法
{
sendUserList();//向所有在线用户发送最新用户列表,包括该用户本身
while(!isstop&&!client.socket.isClosed())//如果套接字是打开的话
{
try{
String type=(String)client.input.readObject();//读取信息类型
if(type.equalsIgnoreCase("聊天信息"))//如果是聊天信息
{
String tosomebody=(String)client.input.readObject();//读取发送对象
String status=(String)client.input.readObject();//读取发送对象
String action=(String)client.input.readObject();//读取发送动作
String message=(String)client.input.readObject();//读取信息内容
String msg=client.username+action+"对"+tosomebody+"说"+message+"\n";//组合聊天信息
if(status.equalsIgnoreCase("悄悄话"))
{
msg="[悄悄话]"+msg;
}
textarea.append(msg);//在区编辑框上显示消息
if(tosomebody.equalsIgnoreCase("全部"))//判断发送对象是否为全部
{
sendToAll(msg);//向所有在线用户转发聊天信息
}
else{//如果是发送给指定的用户
try
{
client.output.writeObject("聊天信息");//发送聊天信息标识
client.output.flush();
client.output.writeObject(msg);//转发今天信息
client.output.flush();
}catch(Exception e)
{;
}
Dan node=userinfolist.find(tosomebody);
if(node!=null)
{
node.output.writeObject("聊天信息");//发送聊天信息标识
node.output.flush();
node.output.writeObject(msg);//转发聊天信息
node.output.flush();
}
}
}
else if(type.equalsIgnoreCase("用户下线"))//处理用户下线请求
{
Dan node=userinfolist.find(client.username);//在用户链表中查找该用户节点
userinfolist.del(node);//删除该用户的节点
String msg="用户"+client.username+"下线\n";
int count=userinfolist.getCount();
combobox.removeAllItems();//重置用户列表组合框
combobox.addItem("全部");
int i=0;
while(i<count)
{
node=userinfolist.find(i);
if(node==null)
{
i++;
continue;
}
combobox.addItem(node.username);
i++;
}
combobox.setSelectedIndex(0);//设置默认的选中项
textarea.append(msg);
jlabel.setText("在线用户"+userinfolist.getCount()+"人\n");
sendToAll(msg);//向所有用户发送该用户下线的通知
sendUserList();
break;
}
}catch(Exception e)
{;
}
}
}
public void sendToAll(String msg)//向所有在线用户转发聊天信息的方法
{
int count=userinfolist.getCount();
int i=0;
while(i<count)//遍历用户链表
{
Dan node=userinfolist.find(i);//根据索引查找节点
if(node==null)
{
i++;
continue;
}
try{
node.output.writeObject("聊天信息");//发送聊天信息标识
node.output.flush();
node.output.writeObject(msg);//发送聊天信息内容
node.output.flush();
}catch(Exception e)
{;
}
i++;
}
}
public void sendUserList()//向所有用户发送用户列表
{
String userlist="";
int count=userinfolist.getCount();//得到用户链表元素个数
int i=0;
while(i<count)//遍历用户链表,组织用户列表串
{
Dan node=userinfolist.find(i);
if(node==null)
{
i++;
continue;
}
userlist+=node.username;
userlist+='\n';
i++;
}
i=0;
while(i<count)//遍历用户链表,依次发送用户列表信息
{
Dan node=userinfolist.find(i);
if(node==null)
{
i++;
continue;
}
try
{
node.output.writeObject("用户列表");//发送用户列表标识
node.output.flush();
node.output.writeObject(userlist);//发送用户列表内容
node.output.flush();
}catch(Exception e)
{;
}
i++;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -