Skip to content

Commit

Permalink
更新 netty
Browse files Browse the repository at this point in the history
  • Loading branch information
0xcaffebabe committed May 25, 2020
1 parent 5989a3d commit 3d841bf
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions 编程语言/JAVA/框架/netty.md
Expand Up @@ -133,6 +133,28 @@ public class TimeClientHandler extends ChannelInboundHandlerAdapter {

使用解码器

```java
public class TimeDecoder extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
// 如果缓冲区没有足够的数据,不进行处理,只有缓冲区累积一定的数据时,才将数据添加到out
if (in.readableBytes() < 4){
return;
}
// 添加到out后,代表解码器成功解码了一条消息
out.add(in.readBytes(4));
}
}
```
```java
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new TimeDecoder(),new TimeClientHandler());
}
});
```

## 服务端示例

- 依赖
Expand Down

0 comments on commit 3d841bf

Please sign in to comment.