博客
关于我
java仿QQ通信项目四(客户信息和客户端获取好友列表)
阅读量:802 次
发布时间:2019-03-25

本文共 4592 字,大约阅读时间需要 15 分钟。

仿QQ通信系统——实现好友添加与管理功能

在QQ使用过程中,我们都知道可以通过添加好友来建立联系,而好友列表的管理功能也是日常使用中常见的操作。本文将详细描述仿QQ通信系统中好友添加流程及其后台数据结构的实现。

一、项目简介

这个项目旨在模拟QQ的通信功能,主要实现包括:

  • 好友查找与添加:用户可以通过账号查找好友,并发送好友添加请求。
  • 好友管理:支持好友的分类存储、添加与删除等功能。
  • 消息传输:定义与发送不同类型的即时消息,并实现其包装与解包。

接下来,我们将详细分析好友添加功能的实现过程,包括客户端和服务器端的逻辑。


二、好友添加流程详解

1. 好友添加的客户端逻辑

  • 客户端1发送好友查找请求客户端1通过JKnum账号查找好友,此时会发送好友查找信息给服务器。查找好友的方法包括:
  • public static String getUserName(int JKnum) {    UserInfo user = userDB.get(JKnum);    if (user == null) {        return null;    } else {        return user.getnickName();    }}
    1. 服务器响应好友查找结果服务器端查找好友2,并根据好友信息库返回好友昵称和JKnum。

    2. 客户端1发送好友添加请求客户端1收到好友昵称后,会发送好友加入请求,消息包内容包括:

    3. public class addFriendMsg extends MSHead {    private String checkMsg; // 好友验证信息    private String senderName; // 发送方昵称    private String group; // 发送方的分组名    public addFriendMsg(int len, byte type, int dest, int src, String checkMsg, String senderName, String group) {        super(len, type, dest, src);        this.checkMsg = checkMsg;        this.senderName = senderName;        this.group = group;    }    // ...其他方法}
      1. 服务器转发好友添加请求服务器接收到好友申请后,会将请求转发给相应的分组处理。具体流程如下:
        • 若同意:将双方在对方的分组中添加好友,并返回确认消息。
        • 若拒绝:返回拒绝消息。
        1. 客户端2处理好友申请客户端2收到好友加入请求后,会根据验证信息决定是否接受。

        2. 三、后台数据结构设计

          1. 用户信息存储

          // 每个用户的信息存储包括:private HashMap
          > groups = new HashMap<>(); // 好友分组信息private ArrayList
          groupName = new ArrayList<>(); // 分组名称列表// 添加好友分组public void addGroup(String groupName) { this.groupName.add(groupName); this.groups.put(groupName, new ArrayList<>());}// 删除好友分组public void deleteGroup(String groupName) { this.groupName.remove(groupName); this.groups.remove(groupName);}

          2. 好友管理逻辑

          // 新增好友public void addFriend(int friendJKnum, String group) {    if (this.groups.containsKey(group)) {        this.groups.get(group).add(friendJKnum);    }}

          3. 好友查找与互加

          // 查找好友public static String getUserName(int JKnum) {    UserInfo user = userDB.get(JKnum);    if (user == null) {        return null;    } else {        return user.getnickName();    }}// 互加好友public static void addFriend(int JKnum1, int JKnum2, String group1, String group2) {    // 获取用户信息    UserInfo user1 = userDB.get(JKnum1);    UserInfo user2 = userDB.get(JKnum2);    // 增加好友    user1.addFriend(JKnum2, group1);    user2.addFriend(JKnum1, group2);}

          四、消息打包与解包

          1. 消息类型定义

          public enum MSGType {    // 好友查询消息    SEARCH(0x08),    // 好友查询应答消息    SEARCH_RES(0x18),    // 好友申请消息    ADD_FRIEND(0x09),    // 好友申请应答消息    ADD_FRIEND_RES(0x19),    // 其他消息类型    USER_INFO(0x03);}

          2. 消息包装

          // 消息打包逻辑public static byte[] packMsg(Message msg) {    MsHead head = (MsHead) msg;    byte[] bytes = new byte[head.getLength()];    System.arraycopy(head.getBytes(), 0, bytes, 0, head.getLength());    return bytes;}

          3. 消息解包

          // 消息解包逻辑public Message unpackMsg(byte[] data) {    try {        int len = data[0] & 0xFF;        int type = data[1] & 0xFF;        int dest = data[2] & 0xFF;        int src = data[3] & 0xFF;        Message msg = createMsg(type, dest, src, len);        // 处理消息体        if (type == SEARCH) {            // 解析好友查询消息            int friendInSearch = DataIO.toInt8(data, 4);            msg = new SearchFriendMsg(len, type, dest, src, friendInSearch);        } else if (type == SEARCH_RES) {            // 解析好友查询响应消息            byte result = data[4];            String findName = DataIO.readStr(data, 5, 10);            msg = new SearchFriendResMsg(len, type, dest, src, result, findName);        } else if (type == ADD_FRIEND) {            // 解析好友申请消息            String checkMsg = DataIO.readStr(data, 5, 256);            String senderName = DataIO.readStr(data, 265, 10);            String group = DataIO.readStr(data, 275, 10);            msg = new AddFriendMsg(len, type, dest, src, checkMsg, senderName, group);        } else if (type == ADD_FRIEND_RES) {            // 解析好友申请响应消息            byte res = data[4];            if (res == 0) {                String groupMe = DataIO.readStr(data, 5, 10);                String groupFriend = DataIO.readStr(data, 15, 10);                msg = new AddFriendResMsg(len, type, dest, src, res, groupMe, groupFriend);            } else {                msg = new AddFriendResMsg(len, type, dest, src, res);            }        } else {            // 其他消息类型            msg = super.universalPack(data, 4);        }        return msg;    } catch (Exception e) {        System.out.println("解包失败:" + e.getMessage());        return null;    }}

          五、测试与应用

          1. 测试环境

          • 客户端1:发送好友查询请求并添加好友。
          • 客户端2:接收好友查询请求并响应,最终完成好友添加。

          2. 实现效果展示

          • 客户端1输出:

            已收到好友确认消息,好友名青年莫言已加入你的默认分组。
          • 客户端2输出:

            已收到好友添加请求,用户认证通过,已将对方添加至默认分组。

          通过以上实现,我们成功完成了仿似QQ的好友查询与添加功能。在实际应用中,可以根据需要扩展更多功能,如群组管理员权限、好友黑名单等,为整体的即时通讯系统打下坚实基础。

    转载地址:http://kzdyk.baihongyu.com/

    你可能感兴趣的文章
    @ControllerAdvice+@ExceptionHandler全局处理Controller层异常 及其 原理
    查看>>
    @ControllerAdvice、@ExceptionHandler控制全局Controller异常
    查看>>
    @ControllerAdvice用法
    查看>>
    #VERDI# 关于Verdi使用的几个常用技巧整理
    查看>>
    @Resource注解的使用
    查看>>
    @ResponseBody 和 @RequestBody
    查看>>
    A + B 九度oj
    查看>>
    A DBA’s take on MSCA (Mobile supply chain applications)
    查看>>
    A DBA’s take on MSCA (Mobile supply chain applications)
    查看>>
    A20地址线
    查看>>
    abaqus质量缩放系数取值_ABAQUS的质量缩放
    查看>>
    Access restriction: The type FileURLConnection is not accessible due to restriction
    查看>>
    Accessibility
    查看>>
    08-信息收集之端口收集(总结版)
    查看>>
    15种下载文件的方法&文件下载方法汇总&超大文件下载
    查看>>
    anaconda、python卸载后重装以及anaconda--443
    查看>>
    AWVS工具太顶了,漏洞扫描工具AWVS介绍及安装教程
    查看>>
    CentOS 系列:CentOS 7 使用 virt-install + vnc 图形界面/非图形界面 创建虚拟机
    查看>>
    CentOS 系列:CentOS 7文件系统的组成
    查看>>
    CentOS系列:【Linux】CentOS7操作系统安装nginx实战(多种方法,超详细)
    查看>>