我想在 Netty 客户端/服务器上应用压缩/解压缩我在客户端和服务器中使用以下代码进行管道:
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
8192, Delimiters.lineDelimiter()));
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("gzipdeflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
pipeline.addLast("gzipinflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));
// and then business logic.
pipeline.addLast("handler", new NettyClientHandler());
}
和服务器为:
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
8192, Delimiters.lineDelimiter()));
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("gzipdeflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
pipeline.addLast("gzipinflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));
//GlibDecoder
//pipeline.addLast("decoder", new ZlibDecoder());
//pipeline.addLast("encoder", new StringEncoder());
// and then business logic.
pipeline.addLast("handler", new NettyServerHandler());
}
我在客户端启动连接时收到以下错误
警告:无法初始化通道。关闭:[id: 0x3553bb5c] java.lang.NoClassDefFoundError: com/jcraft/jzlib/Inflater at io.netty.handler.codec.compression.JZlibDecoder.(JZlibDecoder.java:28) at io.netty.handler.codec.compression .ZlibCodecFactory.newZlibDecoder(ZlibCodecFactory.java:86) 在 testChat.NettyClientInitializer.initChannel(NettyClientInitializer.java:36) 在 testChat.NettyClientInitializer.initChannel(NettyClientInitializer.java:21) 在 io.netty.channel.ChannelInitializer.channelRegistered(ChannelInitializer. java:70) 在 io.netty.channel.DefaultChannelHandlerContext.fireChannelRegistered(DefaultChannelHandlerContext.java:174) 在 io.netty.channel 的 io.netty.channel.DefaultChannelHandlerContext.invokeChannelRegistered(DefaultChannelHandlerContext.java:188)。
线程“主”java.nio.channels.ClosedChannelException 中的异常
客户端/服务器在没有压缩的情况下工作正常我尝试将压缩/解压缩放在字符串编码之前,但我得到了同样的错误?请问有什么帮助吗?