4 Star 23 Fork 10

HongZe / Netty聊天室

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

Netty高性能聊天室

简要介绍

高性能聊天室项目,主要由用户客户端,管理客户端,通信服务器,业务服务器构成。 其高性能主要体现在

  • 高并发:通信端基于Netty架构,NIO多路复用与分组线程池实现多线程下的可靠性
  • 高速度:通信端与业务端解耦,由RocketMQ通信与异步解耦;将通信服务器大量耗时的在线消息储存,离线消息储存和业务端的大量日志储存通过消息队列中转,使通信端减少阻塞,提高消息响应时间,增加系统整体的可靠性。
  • 高可用:多模块开发,通信服务器不依赖于其他模块,其需要的所有信息由前端拉取业务服务器后发送。可支持多机部署(暂未实现)。

架构图

avator

主要技术栈

  • 前端
    • vue相关技术栈
      • vue-cli
      • vuex
      • vue-router
    • element-UI
    • axios
    • 管理客户端基于开源项目 vue-admin-templete
  • 后端
    • maven多模块开发
    • springboot
    • RocketMQ
    • Netty
    • mybatis
    • mysql

已经实现的功能

- 用户端
    - 登陆
    - 注册
    - 在线私聊
    - 私聊离线消息
    - 私聊消息漫游
    - 添加好友
    - 删除好友
    - 在线群组消息
    - 部分群组消息漫游
    - 添加群组
    - 添加群组成员
- 管理端
    - 日志查看
    - 消息查看
    - 好友查看
    - 群组查看

运行方法

  • 前端:
    • 更改请求地址
    • npm install
    • npm run serve(客户端)
    • npm run dev(管理端)
  • 后端:
    • 部署数据库(在relate_notes下)
    • 部署rocketMQ
    • 更改两者配置
    • 运行admin-serve模块(业务服务器)和serve模块(通信服务器)

问题记录

  • ch.pipeline().addLast(HttpRequestHandler.INSTANCE);
    ch.pipeline().addLast(RegisterRequestHandler.INSTANCE);
    ch.pipeline().addLast(MessageRequestHandler.INSTANCE);
    ch.pipeline().addLast(CreateGroupRequestHandler.INSTANCE);
    ch.pipeline().addLast(GroupMessageRequestHandler.INSTANCE);
    ch.pipeline().addLast(HeartBeatRequestHandler.INSTANCE);
    ch.pipeline().addLast(ExceptionHandler.INSTANCE);
    • HttpRequestHandler 关于使用那个handler,通过前端传回控制字符控制,在第一个处理器(HttpHandler)中获取并添加到传输的包中
    • RegisterRequestHandler 从chanel中获取用户信息并注册到本地用户池???前置处理
  • Websocket前后端传输的协议解析 type: prama

  • handler责任链模式 设计模式 handler传入的参数类型必须与继承的父类传入的泛型一致,否则不会被执行;可以借此来选择handler

  • 关于netty处理器继承的SimpleChannelInboundHandler<T> 关于其传入的泛型

  • json字符串与json对象的转换(前端) https://www.cnblogs.com/chengxs/p/8289890.html https://www.cnblogs.com/chengxs/p/8656723.html

  • json字符串与json对象的转换(后端) 见Mysocket类

  • @Sharable不是线程安全的,多个channel共享一个handler实例

  • TextWebSocketFrame 分多帧传输,在责任链第一条链分流 这里运用了什么设计模式?

  • 前后端交互的方式

    • 
      

    socket.send(JSON.stringify(data));

    将json对象转变为json字符串以传输 后端将json字符串转变为json对象,再转变为bean

    // 将json对象转换为实体类 User user = JSON.parseObject(parmas.toJSONString(), User.class); registerRequestPacket.setUser(user);

    
    
  • 如果直接用ajax/axios传后端用@responsebody接收自动转换

    和上一中方式的联系?是不是一样的?hander中的content-type参数

  • 前后端鉴权和token暂未添加

  • 消息队列的问题,传岑问题暂未解决

  • 跨域问题

    • 后端使用@CrossOrigin注解
    • 前端使用注意添加http/https
  • 使用element-UI

    • vue文件中可以使用全局引用的方式(毕竟是在vue的入口文件中引用的...)
    • js文件中需要额外import
    • main.js为vue架构的入口文件,app.vue为跟组件,在界面开始运行时挂载在index.html上
  • 清楚前端的默认样式:在入口文件中引入类似于reset.css的样式清楚文件

  • 数据改错 resultMap的column要和数据库的字段名对应上

  • 通信服务器端和客户端使用json编码传输,前端传输时先转成json字符串,后端收到后转成jsonObject,再转为对应的实体类。后端先用用jsonObject封装,再转成字符,最后byteBuf.writeBytes封装,传回channel

要是不用json字符串做中转怎么办

  • netty globle异常,情况很多

    • 如netty中packet参数不对应,也会报此异常
    • vuex中的state会在某一个组建中使用,而这个状态的初始化是通过异步加载完成的。组件在渲染过程中,获取的state状态为空。也就是说组件在异步完成之前
    • 就已经完成渲染了,导致组件的数据没有来得及渲染。(私聊对象的刷新)
  • 消息队列中储存的默认转为json字符串,consumer中需要把json字符串转为实体类

    • json字符串和json对象的区别在于前者的key有双引号,后者的key没有双引号
  • js中数组添加元素,新元素会覆盖旧元素的问题

    • 问题复现
    data(){
      response:{},
      resList:[]
    }
    change(){
        this.response.id = this.response.id++
        this.response.state = 0
        this.response.message = info.message
        this.response.userName = this.selfInfo.userName
        this.response.date = info.date
        
        console.log("这里是私聊回复处理类:")
        console.log(this.resList)
       
        this.resList.push(response)
    }

    每次添加之后resList中的所有数组元素都是最后一个

    • *原因 每次取出来的值都放在this.response中,因为是在外面的定义的对象,所以每次的地址是一样的,this.resList中保存的是this.response的地址,当最后一次给this.response赋值为789时,由于是同一个地址,所有所有元素的值只是变量名不一样,指针指向的地址一样了
  • 前端群聊创建与群聊逻辑处理,体会后端模块解耦与前后端分离的好处,处处解耦,各信息单独拉取,分别储存,实际简化了逻辑处理

  • 在在线群组消息创建中,当群组中有人没上线时,后上线的人创建的群组(ChannelGroup)会覆盖之前的,此时需要传回新的通信端groupid在前端存储。此时服务端之前的ChannelGroup无人使用,浪费空间。有什么解决方法?

  • 前端创建群组逻辑(vuex中存储消息类型没有规划好,有些混乱)

    • list.vue组件中选择消息群组,vuex当前消息对象状态码更新为9(此处储存的是业务端的groupid)
    • chatroom.vue组件中监听vuex消息对象状态变化,判断是否状态码为9
    • 若为9,则发送创新群组类型的消息,且在creatGroup函数中置状态码为 3,与后端沟通(send中状态再判断)
    • 若创建成功,返回状态码为 4
    • 若返回状态码为 4 或 其他新用户上线更新后端groupchannel,也会向所有的在线用户发送一个4状态码的消息,则更新chatroom.vue组件中的groupId(此处储存的是通信端的groupid)状态,更新为后端回传的最新的(当有新用户上线时,后端会启用新的groupchannel,此时之前的groupid放弃用)
    • 接下来的每次群聊消息发送,都使用的是chatroom.vue下的groupid,而type为9,与vuex中的相同,不需要判断与改变
  • 找bug,netty的handler责任链中只有全局异常处理,当报全局异常错误时可能有多种错误情况,此时建议断点调试

  • 群聊消息如果有人不在,离校消息如何处理暂时未处理。

TextWebSocketFrame是什么, 和普通的string有什么区别, 和json之间如何转换, 可否将如websockethandler中的编码解码操作抽离

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

基于netty和vue的高性能聊天室 展开 收起
Apache-2.0
取消

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/ni-zewen/netty-chat-room.git
git@gitee.com:ni-zewen/netty-chat-room.git
ni-zewen
netty-chat-room
Netty聊天室
master

搜索帮助