(請觀看本人 聊天室總集篇 博文 —— 《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)