Java团队课程设计-socket聊天室(Day3)
写了两天的项目,越写到后面越觉得这个结构太烂了,所以迫不得已只能重新搭建框架
此前代码的冗余度太高,复用率低,于是今天重构项目
重构后:
对Message类进行整理,分出HistoryMessage(历史消息),OfflineMessage(离线消息),OnlineMessage(在线消息)三个消息子类:
解决传输问题
问题1:
原来在ServiceController中将objectInputStream和objectOutputStream设置为static静态变量,这样做会导致多个线程连接服务器时,后一个线程的objectInputStream和objectOutputStream覆盖前一个线程,导致传输出错
解决:
创建每一个连接线程构造函数中传入各个用户对应的输入输出流
问题2:
原来的UserController(客户端)在与服务端交互时使用了多种对象流,导致接受端的判断变得困难且不利于数据的读取
解决:
统一使用ObjectInputStream对象流,在传输过程中,客户端先发送一个对象给服务端(用于判断发送的对象是什么),再发送需要传输的Object。同时,接收方也需要进行两次相应读取操作
下面仅展示服务端的转发和客户端的接收代码:
public void run() {
while (true) {
try {
Object o = ois.readObject();
if (o.getClass().toString().equals("class message.OnlineMessage")) {
OnlineMessage onlineMessage = (OnlineMessage) o;
if (sendMessage(onlineMessage)) {
//正确发送,Type为2,数据库我自己的表Flag为2,别人的表Flag为1
InsertIntoMysql(onlineMessage, 1);
} else {
//存入数据库,自己的表Type为2,对方的表Type为1,数据库我自己的表Flag为2,别人的表Flag为1
InsertIntoMysql(onlineMessage, 2);
}
} else if (o.getClass().toString().equals("class message.HistoryMessage")) {
getHistoryMessage();//发送历史消息
}
else if(o.getClass().toString().equals("class user.normal.impl.UserDocument"))
{
UserDocument userDocument=(UserDocument) ois.readObject();
sendDocument(userDocument);
}
} catch (IOException |ClassNotFoundException e) {
System.out.println(user.getUserName()+"线程意外退出!");
return;
} catch (SQLException e){
System.out.println(user.getUserName()+"此线程数据库异常!");
return;
}
}
}
public void run() {
while(true){
try {
Object object = objectInputStream.readObject();
if(object.equals("Find error in user regist!")){
//注册失败
RegisteJFrame.registrationReturnInformation("Find error in user regist!");
}else if(object.equals("user created successfully!")){
//注册成功
RegisteJFrame.registrationReturnInformation("user created successfully!");
}else if(object.equals("Login successful!")){
Login.changePermission(2);
//登陆成功
}else if(object.equals("Login error!")){
Login.changePermission(1);
}else if(object.getClass().toString().equals("class user.normal.impl.User")){
//返回数据库中列表信息cxxx
User tempUser;
ArrayList<User> personList = new ArrayList<>();
while(true){
tempUser = (User) objectInputStream.readObject();
if(tempUser.getUserName()==null) break;
personList.add(tempUser);
}
ContactJFrame.returnListInformation(personList);
ContactJFrame.setButton();
}else if(object.getClass().toString().equals("class message.OfflineMessage")){
Message tempMessage;
ArrayList<OfflineMessage> offlineMessageArrayList = new ArrayList<>();
while(true){
tempMessage = (Message)objectInputStream.readObject();
if(tempMessage.getSender()==null) break;
OfflineMessage tempOfflineMessage = (OfflineMessage)tempMessage;
offlineMessageArrayList.add(tempOfflineMessage);
ContactJFrame.changeButton(tempMessage);
}
ContactJFrame.returnOfflineMessage(offlineMessageArrayList);
}else if(object.getClass().toString().equals("class message.OnlineMessage")){
OnlineMessage onlineMessage = (OnlineMessage)objectInputStream.readObject();
ContactJFrame.returnOnlineMessage(onlineMessage);
}else if(object.getClass().toString().equals("class message.HistoryMessage")){
Object o;
ArrayList<HistoryMessage> historyMessageArrayList = new ArrayList<>();
while(true){
o = objectInputStream.readObject();
HistoryMessage t = (HistoryMessage)o;
if(t.getSender()==null) break;
historyMessageArrayList.add(t);
}
ContactJFrame.returnHistoryList(historyMessageArrayList);
}
else if(object.getClass().toString().equals("class user.normal.impl.UserDocument"))
{
Object o=objectInputStream.readObject();
UserDocument userDocument=(UserDocument) o;
System.out.println(userDocument.toString());
ContactJFrame.receiveUserDocument(userDocument);
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return;
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}