1.前言
2.功能實作
3.模塊劃分
4.使用到知識
5.部分代碼實作
6.運行例圖
1.前言
?? 先說一下這個小專案也算是我在大學做的第一個應該算的上是的專案的專案,前前后后用了20天左右吧,先是用swing寫好了仿QQ界面(界面很丑)最后邏輯實作都是后面斷斷續續加進去的,寫這個專案之前沒有很好的規劃在邏輯實作方面與資料庫邏輯互動過于頻繁,走了很多的彎路
2.功能實作
1.修改功能(密碼、昵稱、個性簽名)
2.添加好友、洗掉好友
3.單聊功能
4.判斷好友是否在線
3.模塊劃分

4.使用的知識
- netty
- swing
- 集合等同步阻塞佇列synchronousQueue
- 資料庫MySQL中的CRUD
- C3p0連接池
- JSON字串
5.部分代碼實作
1.nettyController.java
接收到來自客戶端的訊息,與dao層進行互動
dao層與之資料庫進行互動
修改密碼

添加好友

從添加好友邏輯實作上我走了很多的彎路頻繁的訪問資料庫,這是一件很不好的事情
package chat.Project.controller;
import chat.Project.bean.information;
import chat.Project.constant.EnMsgType;
import chat.Project.dao.*;
import chat.utils.CacheUtil;
import chat.utils.JsonUtils;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.netty.channel.Channel;
import java.util.ArrayList;
import java.util.Iterator;
public class NettyController {
private static UserDao userDao = new UserDaoImpl();
private static informationDao informationDao = new informationDaoImpl();
private static friendDao friendDao = new friendDaoImpl();
public static String processing(String message, Channel channel){
//決議客戶端發送的訊息
ObjectNode jsonNodes = JsonUtils.getObjectNode(message);
String msgtype = jsonNodes.get("msgtype").asText();
if (EnMsgType.EN_MSG_LOGIN.toString().equals(msgtype)){
//登錄操作
return loginOperation(jsonNodes,channel);
}else if (EnMsgType.EN_MSG_MODIFY_SIGNATURE.toString().equals(msgtype)){
//修改簽名
return modifySignature(jsonNodes);
}else if (EnMsgType.EN_MSG_MODIFY_NICKNAME.toString().equals(msgtype)){
//修改昵稱
return modifyNickname(jsonNodes);
}else if (EnMsgType.EN_MSG_GETINFORMATION.toString().equals(msgtype)){
//獲取登錄資訊
return getInformation(jsonNodes);
}else if (EnMsgType.EN_MSG_VERIFY_PASSWORD.toString().equals(msgtype)){
//進行修改密碼
return verifyPasswd(jsonNodes);
}else if (EnMsgType.EN_MSG_CHAT.toString().equals(msgtype)){
//單聊模式
return SingleChat(jsonNodes);
}else if (EnMsgType.EN_MSG_GET_ID.toString().equals(msgtype)){
//獲取id
return getId(jsonNodes);
}else if (EnMsgType.EN_MSG_GET_FRIEND.toString().equals(msgtype)){
//獲取好友串列
return getFriend(jsonNodes);
}else if (EnMsgType.EN_MSG_ADD_FRIEND.toString().equals(msgtype)){
//添加好友
return addFriends(jsonNodes);
}else if (EnMsgType.EN_MSG_DEL_FRIEND.toString().equals(msgtype)){
//洗掉好友
return delFriend(jsonNodes);
}else if (EnMsgType.EN_MSG_ACTIVE_STATE.toString().equals(msgtype)){
//判斷好友的在線狀態
return friendIsActive(jsonNodes);
}
return "";
}
//判斷好友在線狀態
private static String friendIsActive(ObjectNode jsonNodes) {
int friendId = jsonNodes.get("friendId").asInt();
ObjectNode objectNode = JsonUtils.getObjectNode();
objectNode.put("msgtype",EnMsgType.EN_MSG_ACK.toString());
objectNode.put("srctype",EnMsgType.EN_MSG_ACTIVE_STATE.toString());
//客戶端保證用戶獨立存在且是好友
Channel channel = CacheUtil.get(friendId);
//判斷用戶是否在線
if (channel == null){
//用戶不在線
objectNode.put("code",200);
}else {
//用戶在線
objectNode.put("code",300);
}
return objectNode.toString();
}
//添加好友
private static String delFriend(ObjectNode jsonNodes) {
Integer friendId = jsonNodes.get("friendId").asInt();
int userId = jsonNodes.get("id").asInt();
String localName = jsonNodes.get("localName").asText();
//封裝發回客戶端的JSON
ObjectNode objectNode = JsonUtils.getObjectNode();
objectNode.put("msgtype",EnMsgType.EN_MSG_ACK.toString());
objectNode.put("srctype",EnMsgType.EN_MSG_DEL_FRIEND.toString());
objectNode.put("code",200);
//驗證是否存在當前好友
information information = informationDao.getInformation(friendId);
String friendName = information.getNickname();
//查詢自己是否有該好友
boolean exist = friendDao.isExist(friendName,userId);
if (exist){
//存在當前好友進行洗掉操作
friendDao.delFriend(userId,friendName);
friendDao.delFriend(friendId,localName);
objectNode.put("code",300);
}
return objectNode.toString();
}
//添加好友
private static String addFriends(ObjectNode jsonNodes) {
Integer friendId = jsonNodes.get("friendId").asInt();
int userId = jsonNodes.get("id").asInt();
String localName = jsonNodes.get("localName").asText();
//驗證是否有ID
boolean exists = userDao.verifyExistFriend(friendId);
ObjectNode objectNode = JsonUtils.getObjectNode();
objectNode.put("msgtype",EnMsgType.EN_MSG_ACK.toString());
objectNode.put("srctype",EnMsgType.EN_MSG_ADD_FRIEND.toString());
objectNode.put("code",200);
if(exists){
//表示存在此id
objectNode.put("code",300);
//獲取好友昵稱
information information = informationDao.getInformation(friendId);
String friendNickname = information.getNickname();
//進行添加好友的操作 兩個對應的資訊都應該添加
friendDao.addFriends(userId,localName,friendNickname);
friendDao.addFriends(friendId,friendNickname,localName);
}
return objectNode.toString();
}
//獲取好友串列
private static String getFriend(ObjectNode jsonNodes) {
int uid = jsonNodes.get("uid").asInt();
//回傳ArrayLis集合
ArrayList<String> friends = friendDao.getFriends(uid);
//封裝JSON
ObjectNode objectNode = JsonUtils.getObjectNode();
objectNode.put("msgtype",EnMsgType.EN_MSG_ACK.toString());
objectNode.put("srctype",EnMsgType.EN_MSG_GET_FRIEND.toString());
//寫回friend集合
Iterator<String> iterator = friends.iterator();
int i = 0;
while (iterator.hasNext()){
objectNode.put("res"+i,iterator.next());
i++;
}
//記錄好友個數
objectNode.put("count",i);
return objectNode.toString();
}
//獲取id
private static String getId(ObjectNode jsonNodes) {
String nickname = jsonNodes.get("nickname").asText();
information information = informationDao.nicknameGetId(nickname);
//聯系人的id
int uid = information.getUid();
//封裝JSON
ObjectNode objectNode = JsonUtils.getObjectNode();
objectNode.put("msgtype",EnMsgType.EN_MSG_ACK.toString());
objectNode.put("srctype",EnMsgType.EN_MSG_GET_ID.toString());
objectNode.put("uid",uid);
return objectNode.toString();
}
//單聊模式
private static String SingleChat(ObjectNode jsonNodes) {
int id = jsonNodes.get("id").asInt();
//根據id在friend表獲取登錄用戶名
//封裝JSON資料服務端轉發資料
ObjectNode objectNode = JsonUtils.getObjectNode();
objectNode.put("msgtype",EnMsgType.EN_MSG_ACK.toString());
objectNode.put("srctype",EnMsgType.EN_MSG_CHAT.toString());
//客戶端保證用戶獨立存在且是好友
Channel channel = CacheUtil.get(id);
//判斷用戶是否在線
if (channel == null){
//用戶不在線
objectNode.put("code",200);
}else{
//用戶在線
objectNode.put("code",300);
//訊息轉發
channel.writeAndFlush(jsonNodes.toString());
}
return objectNode.toString();
}
//修改密碼
private static String verifyPasswd(ObjectNode jsonNodes) {
int id = jsonNodes.get("id").asInt();
String oldPasswd = jsonNodes.get("oldPasswd").asText();
String newPasswd = jsonNodes.get("newPasswd").asText();
boolean exits = userDao.verifyPassword(oldPasswd, id);
ObjectNode objectNode = JsonUtils.getObjectNode();
objectNode.put("msgtype",EnMsgType.EN_MSG_VERIFY_PASSWORD.toString());
objectNode.put("code",200);
if (exits){
//驗證成功
userDao.modifyPasswd(newPasswd,id);
objectNode.put("code",300);
}
return objectNode.toString();
}
//獲取資訊
private static String getInformation(ObjectNode jsonNodes) {
int id = jsonNodes.get("id").asInt();
information information = informationDao.getInformation(id);
//封裝JSON發回客戶端
ObjectNode objectNode = JsonUtils.getObjectNode();
objectNode.put("msgtype",EnMsgType.EN_MSG_ACK.toString());
objectNode.put("srctype",EnMsgType.EN_MSG_GETINFORMATION.toString());
objectNode.put("Nickname",information.getNickname());
objectNode.put("Signature",information.getSignature());
return objectNode.toString();
}
//修改昵稱
private static String modifyNickname(ObjectNode jsonNodes) {
int id = jsonNodes.get("id").asInt();
String nickname = jsonNodes.get("nickname").asText();
//進行存盤
informationDao.storeNickname(nickname,id);
return "";
}
//修改簽名
private static String modifySignature(ObjectNode jsonNodes) {
int id = jsonNodes.get("id").asInt();
String signature = jsonNodes.get("signature").asText();
//進行存盤
informationDao.storeSignature(signature,id);
return "";
}
//登錄操作
private static String loginOperation(ObjectNode objectNode,Channel channel) {
int id = objectNode.get("id").asInt();
String passwd = objectNode.get("passwd").asText();
//進行資料庫查詢
boolean exits = userDao.getInformation(id, passwd);
ObjectNode jsonNodes = JsonUtils.getObjectNode();
jsonNodes.put("msgtype",EnMsgType.EN_MSG_ACK.toString());
jsonNodes.put("srctype",EnMsgType.EN_MSG_LOGIN.toString());
jsonNodes.put("code",300);
//回傳狀態碼
if (exits){
jsonNodes.put("code",200);
//添加用戶的在線資訊
CacheUtil.put(id,channel);
}
return jsonNodes.toString();
}
}
2.ClientHandler.java
客戶端接受來自服務端回傳的訊息
根據回傳的狀態碼來判斷是否操作成功
package chat.Project.netty;
import chat.Frame.chat.ChatFrame;
import chat.Frame.chat.linkmen;
import chat.Frame.chat.login;
import chat.Project.constant.EnMsgType;
import chat.util.JsonUtils;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.util.concurrent.SynchronousQueue;
public class ClientHandler extends SimpleChannelInboundHandler<String> {
//定義一個同步阻塞佇列狀態碼
public static SynchronousQueue<Object> queue = new SynchronousQueue<>();
public static String Nickname;
public String Signature;
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
}
//客戶端接收資料
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println(msg);
//決議服務端發送的訊息
ObjectNode jsonNodes = JsonUtils.getObjectNode((String) msg);
String msgtype = jsonNodes.get("msgtype").asText();
if (EnMsgType.EN_MSG_ACK.toString().equals(msgtype)) {
String srctype = jsonNodes.get("srctype").asText();
if (EnMsgType.EN_MSG_LOGIN.toString().equals(srctype)) {
//登錄操作
queue.offer(jsonNodes.get("code").asInt());
}else if(EnMsgType.EN_MSG_GETINFORMATION.toString().equals(srctype)){
//存取資訊
Nickname = jsonNodes.get("Nickname").asText();
Signature = jsonNodes.get("Signature").asText();
linkmen.label_1.setText(Nickname);
linkmen.field.setText(Signature);
}else if (EnMsgType.EN_MSG_CHAT.toString().equals(srctype)){
//發送端回傳訊息
queue.offer(jsonNodes.get("code").asInt());
}else if (EnMsgType.EN_MSG_GET_ID.toString().equals(srctype)){
int uid = jsonNodes.get("uid").asInt();
queue.offer(uid);
}else if (EnMsgType.EN_MSG_GET_FRIEND.toString().equals(srctype)){
//獲取登錄用戶的好友
int count = jsonNodes.get("count").asInt();
login.friend = new String[count];
for ( int i = 0;i<count;i++){
login.friend[i] = jsonNodes.get("res"+i).asText();
System.out.println(jsonNodes.get("res"+i));
}
}else if (EnMsgType.EN_MSG_ADD_FRIEND.toString().equals(srctype)){
//添加好友
queue.offer(jsonNodes.get("code").asInt());
}else if (EnMsgType.EN_MSG_DEL_FRIEND.toString().equals(srctype)){
//洗掉好友
queue.offer(jsonNodes.get("code").asInt());
}else if (EnMsgType.EN_MSG_ACTIVE_STATE.toString().equals(srctype)){
//好友在線狀態
queue.offer(jsonNodes.get("code").asInt());
}
}else if (EnMsgType.EN_MSG_VERIFY_PASSWORD.toString().equals(msgtype)){
//修改密碼
int code = 0;
code = jsonNodes.get("code").asInt();
queue.offer(code);
}else if (EnMsgType.EN_MSG_CHAT.toString().equals(msgtype)){
//接收端接受訊息 封裝朋友昵稱
String message = " "+ jsonNodes.get("message").asText();
//聊天顯示框讀取訊息
ChatFrame.sb.append(message+"\n");
ChatFrame.displayTextPanel.setText(ChatFrame.sb.toString());
}
}
}
3.linkmen.java
這是登錄成功的界面
package chat.Frame.chat;
import chat.Frame.operation.alterColumn.changeNickname;
import chat.Frame.operation.alterColumn.changePassword;
import chat.Frame.operation.alterColumn.changeSignature;
import chat.Frame.operation.friendHandle.addFriend;
import chat.Frame.operation.friendHandle.delFriend;
import chat.Frame.tipFrame;
import chat.Project.services.sendServers;
import io.netty.channel.Channel;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
/**
* 聯系人界面
*/
public class linkmen extends JFrame {
//容器
private JFrame frame;
//標簽
private JLabel label_2, label_3, label_4, label;
//昵稱
public static JLabel label_1;
//狀態框
private JComboBox box, box_1, box_2;
//圖片
private ImageIcon icon_1, icon;
//文本
private JTextField field_1;
//個性簽名
public static JTextField field;
//面板
private JPanel panel_1, panel_3, panel;
//滾動面板
public JScrollPane panel_2;
//串列
public static JList list;
//與服務端通信的通道
private Channel channel;
//用戶的id
private Integer id;
//暫存oldPasswd
public static JLabel label_5,label_6;
//好友串列陣列
private String[] fd;
//串列
public static DefaultListModel<String> model;
public linkmen(Integer id, Channel channel,String[] fd) {
this.id = id;
this.channel = channel;
this.fd = fd;
}
public void init() {
//初始化面板1并設定資訊
panel_1 = new JPanel();
panel_1.setLayout(null);
panel_1.setLocation(0, 0);
panel_1.setBorder(BorderFactory.createTitledBorder("資料卡"));
panel_1.setSize(new Dimension(295, 148));
panel_1.setOpaque(false);
//初始化面板3并設定資訊
panel_3 = new JPanel();
panel_3.setLayout(null);
panel_3.setBorder(BorderFactory.createTitledBorder("系統設定"));
panel_3.setLocation(0, 617);
panel_3.setSize(new Dimension(295, 55));
panel_3.setOpaque(false);
//設定頭像標簽
label_2 = new JLabel(new ImageIcon("E:\\聊天軟體\\untitled\\src\\imageSource\\4.png"/>
注冊賬號和忘記密碼沒有添加事件現在就是個擺設
2.聯系人界面

這里面的所有功能都可以使用
3.聊天界面

這個里面表情按鈕沒弄好
4.通信的程序

5.修改操作

6.好友的操作

代碼由于有點多不能全部貼出,希望三連
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/216754.html
標籤:其他
