Netty5.0+ 心跳机制(四)

来源:互联网 发布:下载最快的软件 编辑:程序博客网 时间:2024/05/18 02:15

服务端

package Demo6;import java.net.SocketAddress;import io.netty.bootstrap.ServerBootstrap;import io.netty.channel.Channel;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelHandler;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.ChannelHandlerInvoker;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelOption;import io.netty.channel.ChannelPromise;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.nio.NioServerSocketChannel;import io.netty.handler.codec.string.StringDecoder;import io.netty.handler.codec.string.StringEncoder;import io.netty.handler.timeout.IdleStateHandler;public class XinTiaoServer5 {public static void main(String[] args) {ServerBootstrap bootstrap = new ServerBootstrap();EventLoopGroup boss = new NioEventLoopGroup();EventLoopGroup worker = new NioEventLoopGroup();try {bootstrap.group(boss,worker);bootstrap.channel(NioServerSocketChannel.class);bootstrap.childHandler(new ChannelInitializer<Channel>() {@Overrideprotected void initChannel(Channel ch) throws Exception {ch.pipeline().addLast(new IdleStateHandler(5, 5, 10));ch.pipeline().addLast(new StringDecoder());ch.pipeline().addLast(new StringEncoder());ch.pipeline().addLast(new XinTiaoHandler());}});//tcp参数设置bootstrap.option(ChannelOption.SO_BACKLOG, 1024);bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);bootstrap.childOption(ChannelOption.TCP_NODELAY, true);ChannelFuture future = bootstrap.bind(10100);future.channel().closeFuture().sync();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
package Demo6;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelFutureListener;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.SimpleChannelInboundHandler;import io.netty.handler.timeout.IdleState;import io.netty.handler.timeout.IdleStateEvent;import io.netty.handler.timeout.IdleStateHandler;public class XinTiaoHandler extends SimpleChannelInboundHandler<String>{/* * 消息处理中心 * */@Overrideprotected void messageReceived(ChannelHandlerContext ctx, String msg)throws Exception {System.out.println("msg:"+msg);}/** * 新客户端接入 */@Override@Skippublic void channelActive(ChannelHandlerContext ctx) throws Exception {// TODO Auto-generated method stubsuper.channelActive(ctx);}/** * 新客户端断开 */@Override@Skippublic void channelInactive(ChannelHandlerContext ctx) throws Exception {// TODO Auto-generated method stubsuper.channelInactive(ctx);}/** * 事件触发 */@Override@Skippublic void userEventTriggered(final ChannelHandlerContext ctx, Object evt)throws Exception {// TODO Auto-generated method stub//if(evt instanceof IdleStateHandler){//这个事件是指没有读写的时候出发的。if(((IdleStateEvent) evt).state() == IdleState.ALL_IDLE){ChannelFuture writeAndFlush = ctx.writeAndFlush("you will close");writeAndFlush.addListener(new ChannelFutureListener() {@Overridepublic void operationComplete(ChannelFuture future) throws Exception {// TODO Auto-generated method stubctx.channel().close();}});}}else{super.userEventTriggered(ctx, evt);}}}


原创粉丝点击