diff --git a/src/main/java/com/muyu/netty/client/NettyClientInit.java b/src/main/java/com/muyu/netty/client/NettyClientInit.java index ad895d6..ea85fba 100644 --- a/src/main/java/com/muyu/netty/client/NettyClientInit.java +++ b/src/main/java/com/muyu/netty/client/NettyClientInit.java @@ -9,16 +9,17 @@ import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; -import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.DelimiterBasedFrameDecoder; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; +import io.netty.handler.timeout.IdleStateHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + /** * @author 牧鱼 * @Classname NettyClient @@ -52,6 +53,11 @@ public class NettyClientInit { @Override protected void initChannel(SocketChannel channel) throws Exception { + +// SSLEngine sslEngine = sslContext.createSSLEngine(); +// sslEngine.setUseClientMode(false); //服务器端模式 +// sslEngine.setNeedClientAuth(false); //不需要验证客户端 +// channel.pipeline().addFirst("ssl", new SslHandler(sslEngine)); //分包器 channel.pipeline().addLast( new DelimiterBasedFrameDecoder( @@ -60,6 +66,10 @@ public class NettyClientInit { ) ) ); + // 心跳 + channel.pipeline().addLast("HBeat", new IdleStateHandler( + 20, + 10, 0)); //编码器 channel.pipeline().addLast("encoder", new StringEncoder()); //解码器 @@ -78,10 +88,10 @@ public class NettyClientInit { } catch (Exception e) { e.printStackTrace(); } finally { - //解决意外关闭的情况,重新创建工作组,新的默认工作组 - Common.workerGroup = new NioEventLoopGroup(); //优雅的关闭工作组 Common.workerGroup.shutdownGracefully(); + //解决意外关闭的情况,重新创建工作组,新的默认工作组 + Common.workerGroup = new NioEventLoopGroup(); } }