增加心跳,更改创建顺序

master
18201612589 2021-12-10 20:39:52 +08:00
parent 7cc2adf55a
commit bcdf9f2b7c
1 changed files with 13 additions and 3 deletions

View File

@ -9,16 +9,17 @@ import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption; import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder; import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder; import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* @author * @author
* @Classname NettyClient * @Classname NettyClient
@ -52,6 +53,11 @@ public class NettyClientInit {
@Override @Override
protected void initChannel(SocketChannel channel) throws Exception { 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( channel.pipeline().addLast(
new DelimiterBasedFrameDecoder( new DelimiterBasedFrameDecoder(
@ -60,6 +66,10 @@ public class NettyClientInit {
) )
) )
); );
// 心跳
channel.pipeline().addLast("HBeat", new IdleStateHandler(
20,
10, 0));
//编码器 //编码器
channel.pipeline().addLast("encoder", new StringEncoder()); channel.pipeline().addLast("encoder", new StringEncoder());
//解码器 //解码器
@ -78,10 +88,10 @@ public class NettyClientInit {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
//解决意外关闭的情况,重新创建工作组,新的默认工作组
Common.workerGroup = new NioEventLoopGroup();
//优雅的关闭工作组 //优雅的关闭工作组
Common.workerGroup.shutdownGracefully(); Common.workerGroup.shutdownGracefully();
//解决意外关闭的情况,重新创建工作组,新的默认工作组
Common.workerGroup = new NioEventLoopGroup();
} }
} }