美文网首页
4.长连接之心跳检测

4.长连接之心跳检测

作者: 八颗小牙坏脾气 | 来源:发表于2018-12-14 22:54 被阅读0次

    Server

    public class Server {
        public static final int INET_PORT = 8888;
        public static void main(String[] args) throws Exception {
            EventLoopGroup boss = new NioEventLoopGroup();
            EventLoopGroup worker = new NioEventLoopGroup();
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            try {
                serverBootstrap.group(boss, worker)
                        .channel(NioServerSocketChannel.class)
                        .handler(new LoggingHandler(LogLevel.INFO))
                        .childHandler(new ServerChannelInitializer());
    
                ChannelFuture channelFuture = serverBootstrap.bind(INET_PORT).sync();
                channelFuture.channel().closeFuture().sync();
            } finally {
                boss.shutdownGracefully();
                worker.shutdownGracefully();
            }
        }
    }
    
    public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {
        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast(new IdleStateHandler(5, 10, 8, TimeUnit.SECONDS));
            pipeline.addLast(new ServerChannelHandler());
        }
    }
    
    
    public class ServerChannelHandler extends ChannelInboundHandlerAdapter {
    
        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
            if (evt instanceof IdleStateEvent) {
                IdleStateEvent event = (IdleStateEvent) evt;
                String eventMsg = StringUtil.EMPTY_STRING;
                switch (event.state()) {
                    case READER_IDLE:
                        eventMsg = "读空闲";
                        break;
                    case WRITER_IDLE:
                        eventMsg = "写空闲";
                        break;
                    case ALL_IDLE:
                        eventMsg = "读写空闲";
                        break;
                }
    
                System.out.println(ctx.channel().remoteAddress() + "," + eventMsg);
            }
        }
    }
    

    Client

    public class MyChatClient {
        public static void main(String[] args) throws Exception{
            EventLoopGroup group = new NioEventLoopGroup();
            Bootstrap bootstrap = new Bootstrap();
            try {
                bootstrap.group(group)
                        .channel(NioSocketChannel.class)
                        .handler(new MyChatClientInitializer());
                Channel channel = bootstrap.connect("localhost", 8888).sync().channel();
    
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
                for (; ; ) {
                    channel.writeAndFlush(br.readLine() + "\r\n");
                }
    
            } finally {
                group.shutdownGracefully();
            }
        }
    }
    
    public class MyChatClientInitializer extends ChannelInitializer<SocketChannel> {
        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast(new DelimiterBasedFrameDecoder(4096, Delimiters.lineDelimiter()));
            pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
            pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
            pipeline.addLast(new MyChatClientlHandler());
        }
    }
    
    public class MyChatClientlHandler extends SimpleChannelInboundHandler<String> {
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
            System.out.println(msg);
        }
    }
    

    相关文章

      网友评论

          本文标题:4.长连接之心跳检测

          本文链接:https://www.haomeiwen.com/subject/hngnhqtx.html