1、學(xué)習(xí)idleStateHandler, 用來檢測會話狀態(tài)
2、心跳其實就是一個普通的請求,特點數(shù)據(jù)簡單,業(yè)務(wù)也簡單
3、心跳對于服務(wù)端來說,定時清除閑置會話inactive(netty5) channelclose(netty3)
4、心跳對客戶端來說,用來檢測會話是否斷開,是否重連! 用來檢測網(wǎng)絡(luò)延時!
5、檢測心跳有的通過定時檢測,netty提供了api來解決此問題
下面是netty3針對心跳檢測的API測試
package com.heart;import java.net.InetSocketAddress;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import org.jboss.netty.bootstrap.ServerBootstrap;import org.jboss.netty.channel.ChannelPipeline;import org.jboss.netty.channel.ChannelPipelineFactory;import org.jboss.netty.channel.Channels;import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;import org.jboss.netty.handler.codec.string.StringDecoder;import org.jboss.netty.handler.codec.string.StringEncoder;import org.jboss.netty.handler.timeout.IdleStateHandler;import org.jboss.netty.util.HashedWheelTimer;/** * netty服務(wù)端入門 * */public class Server { public static void main(String[] args) { //服務(wù)類 ServerBootstrap bootstrap = new ServerBootstrap(); //boss線程監(jiān)聽端口,worker線程負(fù)責(zé)數(shù)據(jù)讀寫 ExecutorService boss = Executors.newCachedThreadPool(); ExecutorService worker = Executors.newCachedThreadPool(); //設(shè)置niosocket工廠 bootstrap.setFactory(new NioServerSocketChannelFactory(boss, worker)); final HashedWheelTimer hashedWheelTimer = new HashedWheelTimer(); //設(shè)置管道的工廠 bootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("idle", new IdleStateHandler(hashedWheelTimer, 5, 5, 10)); pipeline.addLast("decoder", new StringDecoder()); pipeline.addLast("encoder", new StringEncoder()); pipeline.addLast("helloHandler", new HelloHandler()); return pipeline; } }); bootstrap.bind(new InetSocketAddress(10101)); System.out.PRintln("start!!!"); }}package com.heart;import org.jboss.netty.channel.ChannelEvent;import org.jboss.netty.channel.ChannelFuture;import org.jboss.netty.channel.ChannelFutureListener;import org.jboss.netty.channel.ChannelHandlerContext;import org.jboss.netty.channel.MessageEvent;import org.jboss.netty.channel.SimpleChannelHandler;import org.jboss.netty.handler.timeout.IdleState;import org.jboss.netty.handler.timeout.IdleStateEvent;public class HelloHandler extends SimpleChannelHandler { @Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { System.out.println(e.getMessage()); } @Override public void handleUpstream(final ChannelHandlerContext ctx, ChannelEvent e) throws Exception { if (e instanceof IdleStateEvent) { if(((IdleStateEvent)e).getState() == IdleState.ALL_IDLE){ System.out.println("需要提醒玩家下線"); //關(guān)閉會話,踢玩家下線 ChannelFuture write = ctx.getChannel().write("hi time out, you will close"); write.addListener(new ChannelFutureListener() { @Override public void OperationComplete(ChannelFuture future) throws Exception { ctx.getChannel().close(); } }); } } else { super.handleUpstream(ctx, e); } }}下面是netty5針對心跳檢測的API測試package com.heart;import io.netty.bootstrap.ServerBootstrap;import io.netty.channel.Channel;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelOption;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;/** * netty5服務(wù)端 * */public class Server { public static void main(String[] args) { //服務(wù)類 ServerBootstrap bootstrap = new ServerBootstrap(); //boss和worker EventLoopGroup boss = new NioEventLoopGroup(); EventLoopGroup worker = new NioEventLoopGroup(); try { //設(shè)置線程池 bootstrap.group(boss, worker); //設(shè)置socket工廠、 bootstrap.channel(NioServerSocketChannel.class); //設(shè)置管道工廠 bootstrap.childHandler(new ChannelInitializer<Channel>() { @Override protected 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 ServerHandler()); } }); //netty3中對應(yīng)設(shè)置如下 //bootstrap.setOption("backlog", 1024); //bootstrap.setOption("tcpNoDelay", true); //bootstrap.setOption("keepAlive", true); //設(shè)置參數(shù),TCP參數(shù) bootstrap.option(ChannelOption.SO_BACKLOG, 2048);//serverSocketchannel的設(shè)置,鏈接緩沖池的大小 bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);//socketchannel的設(shè)置,維持鏈接的活躍,清除死鏈接 bootstrap.childOption(ChannelOption.TCP_NODELAY, true);//socketchannel的設(shè)置,關(guān)閉延遲發(fā)送 //綁定端口 ChannelFuture future = bootstrap.bind(10101); System.out.println("start"); //等待服務(wù)端關(guān)閉 future.channel().closeFuture().sync(); } catch (Exception e) { e.printStackTrace(); } finally{ //釋放資源 boss.shutdownGracefully(); worker.shutdownGracefully(); } }}package com.heart;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;/** * 服務(wù)端消息處理 * */public class ServerHandler extends SimpleChannelInboundHandler<String> { @Override protected void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception { System.out.println(msg); ctx.channel().writeAndFlush("hi"); ctx.writeAndFlush("hi"); } @Override public void userEventTriggered(final ChannelHandlerContext ctx, Object evt) throws Exception { if(evt instanceof IdleStateEvent){ IdleStateEvent event = (IdleStateEvent)evt; if(event.state() == IdleState.ALL_IDLE){ //清除超時會話 ChannelFuture writeAndFlush = ctx.writeAndFlush("you will close"); writeAndFlush.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { ctx.channel().close(); } }); } }else{ super.userEventTriggered(ctx, evt); } } /** * 新客戶端接入 */ @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { System.out.println("channelActive"); } /** * 客戶端斷開 */ @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { System.out.println("channelInactive"); } /** * 異常 */ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); } }
新聞熱點
疑難解答