netty4 斷線重連


轉載:http://www.tuicool.com/articles/B7RzMbY

 

一 實現心跳檢測

原理:當服務端每隔一段時間就會向客戶端發送心跳包,客戶端收到心跳包后同樣也會回一個心跳包給服務端

一般情況下,客戶端與服務端在指定時間內沒有任何讀寫請求,就會認為連接是idle(空閑的)的。此時,客戶端需要向服務端發送心跳消息,來維持服務端與客戶端的鏈接。那么怎么判斷客戶端在指定時間里沒有任何讀寫請求呢?netty中為我們提供一個特別好用的IdleStateHandler來干這個苦差事!

在服務端工作線程中添加:arg0.pipeline().addLast("ping", new IdleStateHandler(25, 15, 10,TimeUnit.SECONDS));


這個處理器,它的作用就是用來檢測客戶端的讀取超時的,該類的第一個參數是指定讀操作空閑秒數,第二個參數是指定寫操作的空閑秒數,第三個參數是指定讀寫空閑秒數,
當有操作操作超出指定空閑秒數時,便會觸發UserEventTriggered事件。所以我們只需要在自己的handler中截獲該事件,然后發起相應的操作即可(比如說發起心跳操作)。
以下是我們自定義的handler中的代碼:

/**
	 * 一段時間未進行讀寫操作 回調
	 */
	@Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { // TODO Auto-generated method stub super.userEventTriggered(ctx, evt); if (evt instanceof IdleStateEvent) { IdleStateEvent event = (IdleStateEvent) evt; if (event.state().equals(IdleState.READER_IDLE)) { //未進行讀操作 System.out.println("READER_IDLE"); // 超時關閉channel ctx.close(); } else if (event.state().equals(IdleState.WRITER_IDLE)) { } else if (event.state().equals(IdleState.ALL_IDLE)) { //未進行讀寫 System.out.println("ALL_IDLE"); // 發送心跳消息 MsgHandleService.getInstance().sendMsgUtil.sendHeartMessage(ctx); } } }


也就是說 服務端在10s內未進行讀寫操作,就會向客戶端發送心跳包,客戶端收到心跳包后立即回復心跳包給服務端,此時服務端就進行了讀操作,
也就不會觸發IdleState.READER_IDLE(未讀操作狀態),
若客戶端異常掉線了,並不能響應服務端發來的心跳包,在25s后就會觸發IdleState.READER_IDLE(未讀操作狀態),
此時服務器就會將通道關閉。

二 客戶端實現斷線重連

原理當客戶端連接服務器時

bootstrap.connect(new InetSocketAddress( serverIP, port));


會返回一個ChannelFuture的對象,我們對這個對象進行監聽。

代碼如下:
 
        
import android.os.Handler;
import android.os.HandlerThread; import android.os.Message; import android.util.Log; import com.ld.qmwj.Config; import com.ld.qmwj.MyApplication; import java.net.InetSocketAddress; import java.nio.charset.Charset; import java.util.concurrent.TimeUnit; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.DelimiterBasedFrameDecoder; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; /** * Created by zsg on 2015/11/21. */ public class MyClient implements Config { private static Bootstrap bootstrap; private static ChannelFutureListener channelFutureListener = null; public MyClient() { } // 初始化客戶端 public static void initClient() { NioEventLoopGroup group = new NioEventLoopGroup(); // Client服務啟動器 3.x的ClientBootstrap // 改為Bootstrap,且構造函數變化很大,這里用無參構造。 bootstrap = new Bootstrap(); // 指定EventLoopGroup bootstrap.group(group); // 指定channel類型 bootstrap.channel(NioSocketChannel.class); // 指定Handler bootstrap.handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { // 創建分隔符緩沖對象 ByteBuf delimiter = Unpooled.copiedBuffer("#" .getBytes()); // 當達到最大長度仍沒找到分隔符 就拋出異常 ch.pipeline().addLast( new DelimiterBasedFrameDecoder(10000, true, false, delimiter)); // 將消息轉化成字符串對象 下面的到的消息就不用轉化了 //解碼 ch.pipeline().addLast(new StringEncoder(Charset.forName("UTF-8"))); ch.pipeline().addLast(new StringDecoder(Charset.forName("GBK"))); ch.pipeline().addLast(new MyClientHandler()); } }); //設置TCP協議的屬性 bootstrap.option(ChannelOption.SO_KEEPALIVE, true); bootstrap.option(ChannelOption.TCP_NODELAY, true); bootstrap.option(ChannelOption.SO_TIMEOUT, 5000); channelFutureListener = new ChannelFutureListener() { public void operationComplete(ChannelFuture f) throws Exception { // Log.d(Config.TAG, "isDone:" + f.isDone() + " isSuccess:" + f.isSuccess() + // " cause" + f.cause() + " isCancelled" + f.isCancelled()); if (f.isSuccess()) { Log.d(Config.TAG, "重新連接服務器成功"); } else { Log.d(Config.TAG, "重新連接服務器失敗"); // 3秒后重新連接 f.channel().eventLoop().schedule(new Runnable() { @Override public void run() { doConnect(); } }, 3, TimeUnit.SECONDS); } } }; } // 連接到服務端 public static void doConnect() { Log.d(TAG, "doConnect"); ChannelFuture future = null; try { future = bootstrap.connect(new InetSocketAddress( serverIP, port)); future.addListener(channelFutureListener); } catch (Exception e) { e.printStackTrace(); //future.addListener(channelFutureListener); Log.d(TAG, "關閉連接"); } } }


監聽到連接服務器失敗時,會在3秒后重新連接(執行doConnect方法)

這還不夠,當客戶端掉線時要進行重新連接

在我們自己定義邏輯處理的Handler中

 

@Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception { Log.d(Config.TAG, "與服務器斷開連接服務器"); super.channelInactive(ctx); MsgHandle.getInstance().channel = null; //重新連接服務器 ctx.channel().eventLoop().schedule(new Runnable() { @Override public void run() { MyClient.doConnect(); } }, 2, TimeUnit.SECONDS); ctx.close(); }
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM