# netty-plus
**Repository Path**: ch-zms/netty-plus
## Basic Information
- **Project Name**: netty-plus
- **Description**: 🦄netty快速使用工具
- **Primary Language**: Java
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2022-01-07
- **Last Updated**: 2022-03-01
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# netty-plus
#### 🌿介绍
netty快速使用工具,希望能够给初学者或者是写测试demo的兄弟们一些帮助
### 🍃模块介绍
| 模块名 | 模块介绍 |
| ----------------- | -------------- |
| netty-plus-client | 客户端快速使用 |
| netty-plus-server | 服务端快速使用 |
| netty-plus-core | 核心模块 |
| netty-client-test | 客户端测试示例 |
| netty-test | 服务端测试示例 |
### 🚀快速开始
- 将项目下载下来后,将项目安装到本地maven仓库
- spring boot项目
- 可在application.yml文件中配置参数(参数都有提示)支持默认参数
#### 服务端
- 引入pom文件
```xml
com.ch
netty-plus-server
1.0-SNAPSHOT
```
- 自定义处理器使用的两种方式:
一,通过ServerChannelHandlerFactory来增加
```java
@Configuration
public class ServerConfig implements InitializingBean {
@Autowired
ServerChannelHandlerFactory serverChannelHandlerFactory;
@Override
public void afterPropertiesSet(){
serverChannelHandlerFactory.getServerChannelHandlers().addFirst(new StringDecoder());
serverChannelHandlerFactory.getServerChannelHandlers().addFirst(new StringEncoder());
}
}
```
二、通过实现SimpleChannelHandler接口实现
```java
@Component
public class ACustomizeHandler extends SimpleChannelInboundHandler implements SimpleChannelHandler {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("有客户端连接上来");
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println("客户端:"+msg);
ctx.writeAndFlush("你好");
}
}
```
#### 客户端
- 引入pom文件
```xml
com.ch
netty-plus-client
1.0-SNAPSHOT
```
- 自定义处理器使用的两种方式:
一,通过ServerChannelHandlerFactory来增加
```java
@Configuration
public class ServerConfig implements InitializingBean {
@Autowired
ServerChannelHandlerFactory serverChannelHandlerFactory;
@Override
public void afterPropertiesSet(){
serverChannelHandlerFactory.getServerChannelHandlers().addFirst(new StringDecoder());
serverChannelHandlerFactory.getServerChannelHandlers().addFirst(new StringEncoder());
}
}
```
二、通过实现SimpleChannelHandler接口实现
```java
@Component
public class ACustomizeHandler extends SimpleChannelInboundHandler implements SimpleChannelHandler {
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println(msg);
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush("我是客户端");
System.out.println("连接成功");
}
}
```