【手写dubbo-2】超详细!netty实现群聊、私聊

一、功能背景

巩固netty知识,使用netty完成一个聊天系统,通过该聊天系统更加深入的了解netty。设计知识点:nio、reactor模型、tcp粘包拆包、自定义协议等等。

二、功能描述

  1. 服务端启动,客户端连接服务端。
  2. 服务器记录在线客户端,并且分配客户端userId。
  3. 客户端发送消息。
  4. 服务端根据发发送类型判断群发还是私聊。
  5. 客户端收到消息并且打印消息。

三、功能架构图

四、功能预览

server

client1 群发消息

client2消息接收

client3消息接收

client1发送消息给client3

client3控制台打印

五、代码示例

5.1、pom引入jar

<dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.6.1</version>
        </dependency>
        <!-- 阿里JSON解析器 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.75</version>
        </dependency>

5.2、server端

5.3、自定义协议

public class Content
{
          
   
	private Long userId;
	private String msg;
	//此处省略get、set方法
}

5.4、客户端代码

经验分享 程序员 微信小程序 职场和发展