📄 serverreceive.java
字号:
package control;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import model.Node;
public class ServerReceive extends Thread {
JTextArea textArea;
JLabel showStatus_1;
JComboBox comboBox;
Node client;
UserLinkList userLinkList;
public boolean isStop;
public ServerReceive(JTextArea textArea, JLabel showStatus_1,
JComboBox comboBox, Node client, UserLinkList userLinkList) {
this.textArea = textArea;
this.showStatus_1 = showStatus_1;
this.comboBox = comboBox;
this.client = client;
this.userLinkList = userLinkList;
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) {
// TODO: handle exception
e.printStackTrace();
}
Node node = userLinkList.findUser(toSomebody);
if (node != null) {
node.output.writeObject("聊天信息");
node.output.flush();
node.output.writeObject(msg);
node.output.flush();
}
}
} else if (type.equalsIgnoreCase("用户下线")) {
Node node = userLinkList.findUser(client.username);
userLinkList.delUser(node);
String msg = "用户" + client.username + "下线\n";
int count = userLinkList.getCount();
comboBox.removeAllItems();
comboBox.addItem("所有人");
int i = 0;
while (i < count) {
node = userLinkList.findUser(i);
if (node == null) {
i++;
continue;
}
comboBox.addItem(node.username);
i++;
}
comboBox.setSelectedIndex(0);
textArea.append(msg);
showStatus_1.setText(userLinkList.getCount() + "人");
sendToAll(msg);
sendUserList();
break;
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
public void sendUserList() {
String userList = "";
int count = userLinkList.getCount();
int i = 0;
while (i < count) {
Node node = userLinkList.findUser(i);
if (node == null) {
i++;
continue;
}
userList += node.username;
userList += '\n';
i++;
}
i = 0;
while (i < count) {
Node node = userLinkList.findUser(i);
if (node == null) {
i++;
continue;
}
try {
node.output.writeObject("用户列表");
node.output.flush();
node.output.writeObject(userList);
node.output.flush();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
i++;
}
}
public void sendToAll(String msg) {
int count = userLinkList.getCount();
int i = 0;
while (i < count) {
Node node = userLinkList.findUser(i);
if (node == null) {
i++;
continue;
}
try {
node.output.writeObject("聊天信息");
node.output.flush();
node.output.writeObject(msg);
node.output.flush();
} catch (Exception e) {
// TODO: handle exception
}
i++;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -