(请观看本人 聊天室总集篇 博文 —— 《CSFramework 聊天室》)html
首先,因为咱们来编写model层:java
并且,只要咱们执行 注册/上线/注销 操做,就要在存储客户端信息的地方(通常在公司里,用的是数据库,可是,在此处重点考察的是基本知识点,本人就用一份XML文件来存储用户信息了)
增长/查询/删除 客户端信息数据库
因此,本人就先来编写存储用户信息的类:服务器
package edu.youzg.chat_room.server.user.model; public class UserInfo { private String id; private String nick; private String password; public UserInfo() { } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getNick() { return nick; } public void setNick(String nick) { this.nick = nick; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return nick; } }
那么,咱们再来实现下用于 从数据库(此处是从XML配置文件中)处理用户数据的 dao层:app
本人如今来编写下dao层的 UserDao类:dom
package edu.youzg.chat_room.server.user.dao; import org.w3c.dom.Document; import org.w3c.dom.Element; import edu.youzg.chat_room.server.user.model.UserInfo; import edu.youzg.util.XMLParser; public class UserDao { private Document document; public UserDao() { this.document = XMLParser.getDocument("/student.xml"); } public UserInfo getUser(String id) { UserInfo user = new UserInfo(); new XMLParser() { @Override public void dealElement(Element element, int index) { String strId = element.getAttribute("id"); if (!strId.equals(id)) { return; } String nick = element.getAttribute("nick"); String password = element.getAttribute("password"); user.setId(strId); user.setNick(nick); user.setPassword(password); } }.parseTag(this.document, "student"); if (user.getId() == null) { return null; } return user; } }
如今,咱们来实现下处理客户端请求 的action层:ide
由于本人说过:搭建聊天室,咱们就要处理用户的各类资源请求,
因此,本人如今来编写处理客户端请求的 UserAction类:this
package edu.youzg.chat_room.server.user.action; import edu.youzg.chat_room.server.user.model.UserInfo; import edu.youzg.chat_room.server.user.service.UserService; import edu.youzg.csframework.action.Actioner; import edu.youzg.csframework.action.Argument; import edu.youzg.csframework.action.Mapping; @Actioner public class UserAction { private UserService userService; public UserAction() { this.userService = new UserService(); } @Mapping("userLogin") public UserInfo getUserById( @Argument("id") String id, @Argument("password") String password) { UserInfo user = userService.getUserById(id, password); if (user == null) { user = new UserInfo(); user.setId("ERROR"); } else { user.setPassword(null); } return user; } }
如今,本人再来编写下用于从用户界面上获取用户数据的server层:code
package edu.youzg.chat_room.server.user.service; import edu.youzg.chat_room.server.user.dao.UserDao; import edu.youzg.chat_room.server.user.model.UserInfo; public class UserService { private UserDao userDao; public UserService() { this.userDao = new UserDao(); } public UserInfo getUserById(String id, String password) { UserInfo user = userDao.getUser(id); if (user == null || !password.equals(user.getPassword())) { return null; } return user; } }
那么,如今,本人就来编写下服务器界面的view层:orm
本人如今来编写下 ChatRoomServerView类:
package edu.youzg.chat_room.server.view; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.border.TitledBorder; import edu.youzg.csframework.core.IListener; import edu.youzg.csframework.core.Server; import edu.youzg.util.FrameIsNullException; import edu.youzg.util.IMecView; import edu.youzg.util.PropertiesParser; import edu.youzg.util.ViewTool; public class ChatRoomServerView implements IMecView, IListener { private Server server; private JFrame jfrmServerView; private JTextField jtxtCommand; private JTextArea jtatMessage; public ChatRoomServerView() { this.server = new Server(); this.server.addListener(this); } public ChatRoomServerView initServer() { this.server.initServer("/server.cfg.properties"); initView(); return this; } @Override public void init() { jfrmServerView = new JFrame("右转聊天室"); jfrmServerView.setLayout(new BorderLayout()); jfrmServerView.setMinimumSize(new Dimension(800, 600)); jfrmServerView.setExtendedState(JFrame.MAXIMIZED_BOTH); jfrmServerView.setLocationRelativeTo(null); jfrmServerView.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); JLabel jlblTopic = new JLabel("右转哥粉丝聊天室", JLabel.CENTER); jlblTopic.setFont(topicFont); jlblTopic.setForeground(topicColor); jfrmServerView.add(jlblTopic, BorderLayout.NORTH); JPanel jpnlFooter = new JPanel(new FlowLayout()); jfrmServerView.add(jpnlFooter, BorderLayout.SOUTH); JLabel jlblCommand = new JLabel("命令:"); jlblCommand.setFont(normalFont); jpnlFooter.add(jlblCommand); jtxtCommand = new JTextField(40); jtxtCommand.setFont(normalFont); jpnlFooter.add(jtxtCommand); JPanel jpnlLeftBlank = new JPanel(); JLabel jlblLeftBlank = new JLabel(" "); jlblLeftBlank.setFont(normalFont); jpnlLeftBlank.add(jlblLeftBlank); jfrmServerView.add(jpnlLeftBlank, BorderLayout.WEST); JPanel jpnlRightBlank = new JPanel(); JLabel jlblRightBlank = new JLabel(" "); jlblRightBlank.setFont(normalFont); jpnlRightBlank.add(jlblRightBlank); jfrmServerView.add(jpnlRightBlank, BorderLayout.EAST); jtatMessage = new JTextArea(); jtatMessage.setFont(normalFont); jtatMessage.setEditable(false); jtatMessage.setFocusable(false); JScrollPane jscpMessage = new JScrollPane(jtatMessage); TitledBorder ttbdMessage = new TitledBorder("系统消息"); ttbdMessage.setTitleFont(normalFont); ttbdMessage.setTitleColor(Color.red); ttbdMessage.setTitlePosition(TitledBorder.ABOVE_TOP); ttbdMessage.setTitleJustification(TitledBorder.CENTER); jscpMessage.setBorder(ttbdMessage); jfrmServerView.add(jscpMessage, BorderLayout.CENTER); } @Override public void reinit() { } private void closeServer() { if (server.isStartup()) { ViewTool.showWarnning(jfrmServerView, "服务器还没有宕机!"); return; } try { exitView(); } catch (FrameIsNullException e) { e.printStackTrace(); } } @Override public void dealEvent() { jfrmServerView.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { closeServer(); } @Override public void windowOpened(WindowEvent e) { PropertiesParser property = new PropertiesParser(); String strAutoStartup = property.value("autoStartup"); if (strAutoStartup.equalsIgnoreCase("true")) { try { server.startup(); } catch (IOException e1) { e1.printStackTrace(); } } } }); jtxtCommand.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String command = jtxtCommand.getText().trim(); if (command.length() <= 0) { return; } dealCommand(command); jtxtCommand.setText(""); } }); } private void dealCommand(String command) { if (command.equalsIgnoreCase("startup") || command.equalsIgnoreCase("st")) { try { server.startup(); } catch (IOException e) { ViewTool.showError(jfrmServerView, e.getMessage()); } } else if (command.equalsIgnoreCase("shutdown") || command.equalsIgnoreCase("sd")) { server.shutdown(); } else if (command.equalsIgnoreCase("exit") || command.equalsIgnoreCase("x")) { closeServer(); } else if (command.equalsIgnoreCase("forcedown") || command.equalsIgnoreCase("fd")) { server.forcedown(); } } @Override public JFrame getFrame() { return jfrmServerView; } @Override public void processMessage(String message) { jtatMessage.append(message); jtatMessage.append("\n"); jtatMessage.setCaretPosition(jtatMessage.getText().length()); } }
在文末,本人来给出两个服务器端运行必须的配置文件:
首先是 用于初始化Server的 server.cfg.properties文件:
port=6666 maxClientCount=10 autoStartup=true
而后是 用于存储同窗们信息的student.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <students> <student id="20200001" nick="天蓬" password="1450575459"></student> <student id="20200002" nick="齐天" password="1450575459"></student> <student id="20200003" nick="卷帘" password="1450575459"></student> <student id="20200004" nick="玄奘" password="1450575459"></student> </students>
至于运行结果,本人将放在总集篇为同窗们展现。
(本人 聊天室总集篇 博文 ——《CSFramework 聊天室》连接:
https://www.cnblogs.com/codderYouzg/p/12749222.html)