尚硅谷2019年Netty教程 netty 心跳 ----目标netty---step5.30

// 空闲后会触发 idleStateEvent 事件

// idleStateEvent 事件 会触发 userEventTiggered 方法 处理具体空闲事件
// 详 见 MyServerHandler 中的 userEventTriggered方法
pipeline.addLast(new IdleStateHandler(3,5,7, TimeUnit.SECONDS));
//

  • netty 心跳处理器 服务端 编码
  • https://www.bilibili.com/video/BV1jK4y1s7GV?p=66 ----- 心跳 服务器端 Myserver 编码
  • https://www.bilibili.com/video/BV1jK4y1s7GV?p=67 ----- 心跳 服务器端 MyserverHandler 编码 及测试
  • ------------------------------------------- 客户端编码借助 groupChat 的client
  • 63 服务端 64 客户端及调试
  • https://www.bilibili.com/video/BV1jK4y1s7GV?p=63
  • https://www.bilibili.com/video/BV1jK4y1s7GV?p=64
package com.atguigu.heartbeat;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

/**
 * Author: tz_wl
 * Date: 2020/9/24 9:36
 * Content:
 *
 * netty 心跳处理器  服务端 编码
 * https://www.bilibili.com/video/BV1jK4y1s7GV?p=66    ----- 心跳 服务器端 Myserver 编码
 * https://www.bilibili.com/video/BV1jK4y1s7GV?p=67    ----- 心跳 服务器端 MyserverHandler 编码  及测试
 * ------------------------------------------- 客户端编码借助 groupChat 的client
 *
 * 63 服务端    64 客户端及调试
 * https://www.bilibili.com/video/BV1jK4y1s7GV?p=63
 * https://www.bilibili.com/video/BV1jK4y1s7GV?p=64
 * 代码在  https://blog.csdn.net/wei198621/article/details/108764920
 *
 */
public class MyServer {


    private static int port=8097;

    public static void main(String[] args) {

        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel sc) throws Exception {
                            ChannelPipeline pipeline = sc.pipeline();
                            //IdleStateHandler 处理空闲状态的处理器
                            //
                            //
                            // 空闲后会触发 idleStateEvent 事件
                            // idleStateEvent 事件 会触发 userEventTiggered 方法 处理具体空闲事件
                            // 详 见  MyServerHandler 中的 userEventTriggered 方法
                            pipeline.addLast(new IdleStateHandler(3,5,7, TimeUnit.SECONDS));
                            //
                            pipeline.addLast(new MyServerHandler());

                        }
                    });
            ChannelFuture future = serverBootstrap.bind(port).sync();
            future.channel().closeFuture().sync();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }


    }


}

package com.atguigu.heartbeat;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * Author: tz_wl
 * Date: 2020/9/24 18:42
 * Content:
 */
public class MyServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

        String strEventType="";
        if(evt instanceof IdleStateEvent){
           IdleStateEvent event= (IdleStateEvent)evt;
           switch (event.state()){
               case ALL_IDLE:
                   strEventType="读写空闲";
                   break;
               case READER_IDLE:
                   strEventType="读空闲";
                   break;
               case WRITER_IDLE:
                   strEventType="写空闲";
                   break;
           }
            System.out.println(ctx.channel().remoteAddress()+"--超时时间--"+ strEventType);
            System.out.println(" 服务器处理中。。。");

            //如果不关闭 上述空闲会频发的发送
            //ctx.channel().close();
       }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值