📄 serverreceivethread.java
字号:
/*
* Created on 2005-12-19
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package chatRoom;
import javax.swing.*;
//import java.io.*;
//import java.net.*;
/**
* @author hongyuan
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ServerReceiveThread extends Thread{
JComboBox comboBox; //用户列表框
JTextArea textArea;//聊天信息框
JTextField textField;//提示信息框
UserInfoList userInfoList;//用户信息链表
Node client;
public boolean isStop;
public ServerReceiveThread(
JComboBox comboBox,
JTextArea textArea,
JTextField textField,
UserInfoList userInfoList,
Node client)
{
this.comboBox=comboBox;
this.textArea=textArea;
this.textField=textField;
this.userInfoList=userInfoList;
this.client=client;
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("all"))
{
sendToAll(msg);
}
else//发给指定用户
{
try
{
//发送聊天信息标识
client.output.writeObject("聊天信息");
client.output.flush();
//转发聊天信息
client.output.writeObject(msg);
client.output.flush();
}catch(Exception e){}
Node node=userInfoList.find(toSomebody);
if(node!=null)
{
//发送聊天信息标识
node.output.writeObject("聊天信息");
node.output.flush();
//转发聊天信息
node.output.writeObject(msg);
node.output.flush();
}
}
}
else if(type.equalsIgnoreCase("用户下线"))//处理用户下线请求
{
//在用户链表中查找该节点
Node node=userInfoList.find(client.username);
//删除该节点
userInfoList.del(node);
String msg="用户"+client.username+"下线\n";
int count=userInfoList.getCount();
//重置用户列表组合框
comboBox.removeAllItems();
comboBox.addItem("all");
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);
textField.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)
{
Node 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)//遍历链表,组织用户列表串
{
Node node=userInfoList.find(i);
if(node==null)
{
i++;
continue;
}
userlist+=node.username;
userlist+='\n';
i++;
}
i=0;
while(i<count)//遍历链表,依次发送用户列表信息
{
Node 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 + -