JAVA基于Swing和Netty,仿QQ界面聊天小项目

1.前言

先说一下这个小项目也算是我在大学做得第一个应该算的上是的项目的项目,前前后后用了20天左右吧。先是用swing写好了仿QQ界面(界面很丑)最后逻辑实现都是后面断断续续加进去的。写这个项目之前没有很好的规划在逻辑实现方面与数据库逻辑交互过于频繁。走了很多的弯路

2.功能实现

1.修改功能(密码、昵称、个性签名) 2.添加好友、删除好友 3.单聊功能 4.判断好友是否在线

3.模块划分

编辑切换为居中

添加图片注释,不超过 140 字(可选)

4.使用的知识

  • netty

  • swing

  • 集合等同步阻塞队列synchronousQueue

  • 数据库MySQL中的CRUD

  • C3p0连接池

  • JSON字符串

5.部分代码实现

1.nettyController.java

 
 

接收到来自客户端的消息,与dao层进行交互 dao层与之数据库进行交互 12

修改密码

编辑切换为居中

添加图片注释,不超过 140 字(可选)

添加好友

编辑切换为居中

添加图片注释,不超过 140 字(可选)

从添加好友逻辑实现上我走了很多的弯路频繁地访问数据库,这是一件很不好的事情

 
 

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 o

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值