// 空闲后会触发 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();
}
}
}