netty示例-socket连接实时检测
1.MyServer.java
public class MyServer {
public static void main(String[] args) {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workGroup).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel sc) throws Exception {
ChannelPipeline pipeline = sc.pipeline();
pipeline.addLast(new IdleStateHandler(5, 10, 15, TimeUnit.SECONDS));
pipeline.addLast(new TestMyServerHandler());
}
});
ChannelFuture channelFuture = serverBootstrap.bind(8989).sync();
channelFuture.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
}finally {
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
}
}2.TestMyServerHandler.java
public class TestMyServerHandler extends ChannelInboundHandlerAdapter{
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if(evt instanceof IdleStateEvent){
IdleStateEvent event = (IdleStateEvent)evt;
String eventString = null;
switch(event.state()){
case READER_IDLE: eventString = "读空闲";break;
case WRITER_IDLE: eventString = "写空闲";break;
case ALL_IDLE: eventString ="读写空闲";break;
}
System.out.println(ctx.channel().remoteAddress()+"超时事件"+eventString);
ctx.channel().close();
}
}
}3.MyChatClient.java
public class MyChatClient {
public static void main(String[] args) {
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel sc) throws Exception {
ChannelPipeline pipeline = sc.pipeline();
pipeline.addLast(new DelimiterBasedFrameDecoder(4096,Delimiters.lineDelimiter()));
pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast(new TestMyChatClientHandler());
}
});
Channel channel = bootstrap.connect("localhost",8989).sync().channel();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
for(;;){
channel.writeAndFlush(reader.readLine()+"\r\n");
}//创造死循环,读取键盘输入的信息
} catch (Exception e) {
e.printStackTrace();
}finally {
eventLoopGroup.shutdownGracefully();
}
}
}4.TestMyChatClientHandler.java
public class TestMyChatClientHandler extends SimpleChannelInboundHandler<String>{
@Override
protected void channelRead0(ChannelHandlerContext paramChannelHandlerContext, String msg) throws Exception {
System.out.println(msg);
}
}5.TcpHandler拦截器解答
IdleStateHandler(5, 10, 15, TimeUnit.SECONDS);其中 5 是超过5秒读空闲被触发, 10 是超过10秒写空闲被触发, 15 是超过15秒读写空闲被触发
TimeUnit.SECONDS,指定触发的时间单位。可以是分,小时等等。
(1).心跳机制IdleStateHandler
心跳是在TCP长连接中,客户端和服务端定时向对方发送数据包通知对方自己还在线,保证连接的有效性的一种机制在服务器和客户端之间一定时间内没有数据交互时, 即处于 idle 状态时, 客户端或服务器会发送一个特殊的数据包给对方, 当接收方收到这个数据报文后, 也立即发送一个特殊的数据报文, 回应发送方, 此即一个 PING-PONG 交互. 自然地, 当某一端收到心跳消息后, 就知道了对方仍然在线, 这就确保 TCP 连接的有效性.
(2).心跳实现:
使用TCP协议层的Keeplive机制,但是该机制默认的心跳时间是2小时,依赖操作系统实现不够灵活;
应用层实现自定义心跳机制,比如Netty实现心跳机制;
文章标题:netty示例-socket连接实时检测
发布时间:2020-01-17, 09:52:46
最后更新:2020-01-17, 09:52:46