服务端接收消息解析老是出现消息不全的问题
解码编码器
@Component public class SimpleChatChannelInitializer extends ChannelInitializer{ @Override protected void initChannel(SocketChannel socketChannel) { ChannelPipeline pipeline = socketChannel.pipeline(); ByteBuf bufAck = Unpooled.copiedBuffer("".getBytes()); ByteBuf bufReply = Unpooled.copiedBuffer("".getBytes()); pipeline.addLast(new DelimiterbasedframeDecoder(5 * 1024 * 1024,bufAck,bufReply)); pipeline.addLast(new StringDecoder(StandardCharsets.UTF_8)); pipeline.addLast("stringEncoder", new StringEncoder(CharsetUtil.UTF_8)); pipeline.addLast(new SimpleChatServerHandler()); } }
pipeline.addLast(new DelimiterbasedframeDecoder(5 * 1024 * 1024,bufAck,bufReply));
参数5 * 1024 * 1024 缓存数据大小,bufAck,bufReply 以“”或“”结尾
结合字符串解码器pipeline.addLast(new StringDecoder(StandardCharsets.UTF_8));一起使用。
handler即可使用string处理
public class SimpleChatServerHandler extends SimpleChannelInboundHandler{}



