一、handler定义:
package handler;import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;public class MyChatServerHandler3 extends ChannelInboundHandlerAdapter {@Overridepublic void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {System.out.println(evt);super.userEventTriggered(ctx, evt);}@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {System.out.println("received: " + msg);}
}
二、定义Initializer
package initializer;import handler.MyChatServerHandler;
import handler.MyChatServerHandler3;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.concurrent.GlobalEventExecutor;import java.util.concurrent.TimeUnit;public class MyChatServerInitializer3 extends ChannelInitializer<SocketChannel>{@Overrideprotected void initChannel(SocketChannel ch) throws Exception {System.out.println("in initChannel......");//ch.pipeline().addLast(new DelimiterBasedFrameDecoder(4096,Delimiters.lineDelimiter()));ch.pipeline().addLast(new IdleStateHandler(3, 5, 6));ch.pipeline().addLast(new StringDecoder());ch.pipeline().addLast(new StringEncoder());ch.pipeline().addLast(new MyChatServerHandler3());}}
三、定义server启动类,在pipeline中添加IdleStateHandler
package server;import handler.MyChatServerHandler;
import initializer.MyChatServerInitializer;
import initializer.MyChatServerInitializer;
import initializer.MyChatServerInitializer3;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;public class MyChatServer3 {public static void main(String[] args) throws Exception {EventLoopGroup bossGroup =new NioEventLoopGroup();EventLoopGroup workerGroup =new NioEventLoopGroup();try{ServerBootstrap serverBootstrap=new ServerBootstrap();serverBootstrap.group(bossGroup,workerGroup);serverBootstrap.channel(NioServerSocketChannel.class);serverBootstrap.handler(new LoggingHandler(LogLevel.INFO));serverBootstrap.childHandler(new MyChatServerInitializer3());ChannelFuture channelFuture=serverBootstrap.bind(9999).sync();channelFuture.channel().closeFuture().sync();}finally{bossGroup.shutdownGracefully();workerGroup.shutdownGracefully();}}}
当客户端连接之后,如果一段时间不发送数据,则服务器端会触发IdleStateEvent并被处理