netty中收到的消息是ByteBuf類型,需要先将其轉換為java對象,才能做業務處理。發出消息時則需要先将java對象轉為ByteBuf。解碼器和編碼器就是專門用來處理這種轉換的,收到消息時解碼,發出消息時編碼。
啟動配置
修改netty啟動代碼,現在有三個自定義類,多了解碼器和編碼器。解碼器是入站處理,編碼器是出站處理。
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new MyDecoder());//解碼器
ch.pipeline().addLast(new MyEncoder());//編碼器
ch.pipeline().addLast(new MyHandler());//業務處理
}
}).option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
解碼
繼承MessageToMessageDecoder即可,在這裡将收到的ByteBuf消息轉換成字元串。可能你讀取到的是一個json字元串,你可以在這裡轉換成自定義java類型,然後放入out清單中。如果在out中放入了多個對象,每個對象都會單獨觸發一次業務處理。
public class MyDecoder extends MessageToMessageDecoder<ByteBuf>{
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
String s = msg.toString(CharsetUtil.UTF_8);
out.add(s);
}
業務處理
這裡String類型msg是不能直接發出去的,還需要有編碼器碼轉換成ByteBuf 才能發出。
public class MyHandler extends SimpleChannelInboundHandler<String> {
protected void channelRead0(ChannelHandlerContext ctx, String msg) {
System.out.println(msg);
ctx.writeAndFlush(msg);
}
編碼
把msg字元串寫入ByteBuf,加入out清單中,就可以正常發出消息了。這裡的List和解碼時一樣,加入多個不同的ByteBuf會發出多條消息。
public class MyEncoder extends MessageToMessageEncoder<String>{
@Override
protected void encode(ChannelHandlerContext ctx, String msg, List<Object> out) throws Exception {
ByteBuf b = Unpooled.buffer();
b.writeBytes(msg.getBytes());
out.add(b);
}
預設解碼和編碼
netty中有一些預設的解碼和編碼類,比如字元串解碼StringDecoder和字元串編碼StringEncoder,可以直接使用。啟動類做以下修改,效果和上面是一樣的。
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new StringEncoder());
ch.pipeline().addLast(new MyHandler());
位元組解碼
socket項目的很多場景,如物聯網,需要自己去逐個讀取16進制位元組,根據每個位元組的含義,分别給自定義的java類型參數指派。此時則使用ByteToMessageDecoder解碼,逐字讀取和16進制用法參考第3篇。
public class MyDecoder extends ByteToMessageDecoder{
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
ByteBuf b = in.readBytes(in.readableBytes());
String msg = b.toString(CharsetUtil.UTF_8);
out.add(msg);
}
位元組編碼
位元組編碼和MessageToMessageEncoder類似,但一次隻能發出一條消息。
public class MyEncoder extends MessageToByteEncoder<String>{
@Override
protected void encode(ChannelHandlerContext ctx, String msg, ByteBuf out) throws Exception {
out.writeBytes(msg.getBytes());
}