兰州交通大学博文学院毕业设计(论文)
}
Transaction trans=session.beginTransaction(); session.save(inf); trans.commit(); session.close();
5.4 客户端模块功能实现
图 6-4 客户端界面
客户端时用户聊天的主功能界面,用户登陆后可以实现发送公聊信息、发送私聊信息、接受服务器发送的系统信息;用户也可以对系统消息、个人聊天记录、在线用户信息进行查询,并且可以对个人注册信息进行修改以及删除个人聊天记录。 5.4.1 客户端接受信息功能实现 public void run() {
if (type.equalsIgnoreCase(\系统信息]\)) {
String sysMsg = (String) input.readObject();
if (sysMsg.equalsIgnoreCase(\用户名已被使用\)) {// 若登陆
while (!isStop) {
try {
String type = (String) input.readObject();// 读取信息类型
用户名称已被使用,则停止当前登陆用记信息接收线程
C.logOut();
textArea.append(\系统信息]\ + sysMsg + \); break;
31
兰州交通大学博文学院毕业设计(论文)
}
if (sysMsg.equalsIgnoreCase(\服务器关闭!\)) { }
if (sysMsg.equalsIgnoreCase(\踢出\)) {
C.logOut(); textArea
.append(\系统信息]你违反了本聊天室规则.\\n[系统
C.logOut();
textArea.append(\服务器已关闭!\\n\); break;
信息]被管理员踢出了聊天室.\\n\);
}
textArea.append(\系统信息]\ + sysMsg);
break;
} else if (type.equalsIgnoreCase(\聊天信息]\)) {
String message = type.trim() + (String) input.readObject(); textArea.append(message);
} else if (type.equalsIgnoreCase(\用户列表]\)) {
String userList = (String) input.readObject(); String userNames[] = userList.split(\); comboBox.removeAllItems(); int i = 0;
comboBox.addItem(\大家\); while (i < userNames.length) { }
comboBox.setSelectedIndex(0);
comboBox.addItem(userNames[i]); i++;
} else if (type.equalsIgnoreCase(\在线用户]\)) {
String sysMsg = type.trim() + (String) input.readObject();
32
兰州交通大学博文学院毕业设计(论文)
} catch (Exception e) { }
}
textField.setText(sysMsg);
5.4.2 客户端发送消息功能实现 public void SendMessage() {
String toSomebody = comboBoxUserList.getSelectedItem().toString(); String status = \; if (socket.isClosed())
return;
if (checkBox.isSelected())
status = \悄悄话\;
String action = comboBoxAction.getSelectedItem().toString(); String content = sendMessage.getText(); Date sendDate=new Date();
Message message=new Message(); message.setContent(content); message.setSendid(userId); message.setSendDate(sendDate); message.setSendToSomebody(toSomebody); try {
ServerServiceImp.saveMessage(message);
System.out.println(\); output.writeObject(\聊天信息]\); output.flush();
output.writeObject(toSomebody); output.flush();
output.writeObject(status);
33
兰州交通大学博文学院毕业设计(论文)
}
output.flush();
output.writeObject(action); output.flush();
output.writeObject(content); output.flush();
} catch (Exception e) { }
5.4.3 查询系统信息功能实现 //对系统消息的查找
public static String getInformation() { }
UserDAOOper oper=OperatorFactory.getUserOperator(); List
return \;
String infStr=\;
Date sendDate=new Date(); for(Information inf:list) { }
return infStr;
sendDate=inf.getSendDate(); String content=inf.getContent(); infStr+=\系统消息\+\
+sendDate.toString()+\ +content+\;
5.4.4 查询聊天记录功能实现
public static String getMessage(Long userid){
34
兰州交通大学博文学院毕业设计(论文)
}
UserDAOOper oper=OperatorFactory.getUserOperator(); List
String message=\; Date sendDate=new Date(); for(Message me:list){ }
return message;
sendDate=me.getSendDate(); String content=me.getContent(); List
String userName=user.get(0).getUserName(); String sendToSomebody=me.getSendToSomebody(); message+=\聊天记录\+\
+sendDate.toString()+\ +userName+\对\
+sendToSomebody+\说\+\ +content +\ +\;
return \没有相关记录\;
5.4.5 用户资料查询功能实现 //查找个人信息
public static String getDescription(String userName) {
UserDAOOper oper=OperatorFactory.getUserOperator(); Long id=null;
id=oper.load(userName).get(0).getId();
35