我想在发送实际消息之前发送我自己的自定义消息,第一次初始化频道。我有:
public class MyClientInitializerFactory extends ClientInitializerFactory {
@Override
protected void initChannel(Channel ch) {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new EncoderTwo());
pipeline.addLast(new EncoderOne());
}
}
编码器一:
public class EncoderOne extends MessageToMessageEncoder<Object> {
@Override
protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
byte[] bytes = "ABCD".getBytes(StandardCharsets.UTF_8);
ByteBuf byteBuf = Unpooled.buffer(bytes.length);
Channel channel = ctx.channel();
channel.writeAndFlush(byteBuf);
out.add(msg); // retaining original message for further processing
ctx.pipeline().remove(this);
}
EncoderTwo 处理实际消息并将其通过网络发送,但我的消息从未到达那里。当我尝试发送任何内容时,这些行会一遍又一遍地执行,直到我得到 StackOverflow:
ByteBuf byteBuf = Unpooled.buffer(bytes.length);
Channel channel = ctx.channel();
channel.writeAndFlush(byteBuf);
我做错了什么?
狐的传说
相关分类