首先我們看一下主體的一個結構:
maven:
<properties>
<java.version>1.8</java.version>
<netty.version>4.1.37.Final</netty.version>
<fastjson.version>1.2.46</fastjson.version>
<msgpack.version>0.6.12</msgpack.version>
<mybatisPlus.version>3.1.1</mybatisPlus.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>${netty.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>org.msgpack</groupId>
<artifactId>msgpack</artifactId>
<version>${msgpack.version}</version>
</dependency>
</dependencies>
client 用戶端:
ChatReqHandler: 聊天請求handler
HeartBeatReqHandler: 心跳請求handler
LoginAuthReqHandler: 登入請求handler
NettyClient: 用戶端請求
NettyClientTwo: 用戶端請求 用于測試第二使用者
server 服務端:
LoginAuthRespHandler: 登入響應handler
ChatRespHandler: 聊天響應handler
HeartBeatRespHandler: 心跳響應handler
LoginAuthRespHandler: 登入響應handler
NettyServer: 服務端響應
最終實作效果:
代碼已上傳至github:https://github.com/beibeirenzhe/netty-im/tree/master/fyrtim