add HeartBeatClientHandler

This commit is contained in:
wujiawei 2023-03-02 23:44:59 +08:00
parent 4e93baf49c
commit d2cf3944ab
8 changed files with 92 additions and 3 deletions

View File

@ -0,0 +1,25 @@
package com.lazy.netty.proxy.client.proxy.handler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import java.util.Date;
public class HeartBeatClientHandler extends ChannelInboundHandlerAdapter {
private int lossConnectCount = 0;
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
System.out.println("客户端循环心跳监测发送: " + new Date());
if (evt instanceof IdleStateEvent) {
IdleStateEvent event = (IdleStateEvent) evt;
if (event.state() == IdleState.WRITER_IDLE) {
ctx.writeAndFlush("biubiu");
}
}
}
}

View File

@ -1,6 +1,8 @@
package com.lazy.netty.proxy.client.proxy.netty;
package com.lazy.netty.proxy.client.proxy.handler;
import com.lazy.netty.proxy.client.proxy.netty.Constant;
import com.lazy.netty.proxy.client.proxy.netty.RealSocket;
import com.lazy.netty.proxy.msg.MyMsg;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;

View File

@ -1,6 +1,7 @@
package com.lazy.netty.proxy.client.proxy.netty;
package com.lazy.netty.proxy.client.proxy.handler;
import com.lazy.netty.proxy.client.proxy.netty.Constant;
import com.lazy.netty.proxy.msg.MyMsg;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;

View File

@ -1,6 +1,8 @@
package com.lazy.netty.proxy.client.proxy.netty;
import com.lazy.netty.proxy.client.proxy.handler.HeartBeatClientHandler;
import com.lazy.netty.proxy.client.proxy.handler.ProxyHandler;
import com.lazy.netty.proxy.msg.MyMsg;
import com.lazy.netty.proxy.msg.MyMsgDecoder;
import com.lazy.netty.proxy.msg.MyMsgEncoder;
@ -61,6 +63,7 @@ public class ProxySocket {
pipeline.addLast(new MyMsgEncoder());
pipeline.addLast(new IdleStateHandler(40, 8, 0));
pipeline.addLast(new ProxyHandler());
pipeline.addLast(new HeartBeatClientHandler());
}
});
@ -68,6 +71,7 @@ public class ProxySocket {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
System.out.println("连接服务端成功");
// 客户端链接代理服务器成功
Channel channel = future.channel();
if (StringUtil.isNullOrEmpty(vid)) {
@ -95,6 +99,20 @@ public class ProxySocket {
realChannel.config().setOption(ChannelOption.AUTO_READ, true);
}
}
} else {
System.out.println("每隔2s重连....");
future.channel().eventLoop().schedule(new Runnable() {
@Override
public void run() {
try {
newConnect(null);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, 2, TimeUnit.SECONDS);
}
}
});

View File

@ -1,5 +1,6 @@
package com.lazy.netty.proxy.client.proxy.netty;
import com.lazy.netty.proxy.client.proxy.handler.RealHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;

View File

@ -2,6 +2,8 @@ package com.lazy.netty.proxy.server;
import com.lazy.netty.proxy.msg.MyMsgDecoder;
import com.lazy.netty.proxy.msg.MyMsgEncoder;
import com.lazy.netty.proxy.server.handler.ClientHandler;
import com.lazy.netty.proxy.server.handler.HeartBeatServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
@ -32,6 +34,7 @@ public class ServerSocket {
pipeline.addLast(new MyMsgEncoder());
pipeline.addLast(new IdleStateHandler(40, 10, 0));
pipeline.addLast(new ClientHandler());
pipeline.addLast(new HeartBeatServerHandler());
}
});

View File

@ -1,7 +1,8 @@
package com.lazy.netty.proxy.server;
package com.lazy.netty.proxy.server.handler;
import com.lazy.netty.proxy.msg.MyMsg;
import com.lazy.netty.proxy.server.Constant;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;

View File

@ -0,0 +1,38 @@
package com.lazy.netty.proxy.server.handler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
public class HeartBeatServerHandler extends ChannelInboundHandlerAdapter {
private int lossConnectCount = 0;
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
System.out.println("已经5秒未收到客户端的消息了");
if (evt instanceof IdleStateEvent){
IdleStateEvent event = (IdleStateEvent)evt;
if (event.state()== IdleState.READER_IDLE){
lossConnectCount++;
if (lossConnectCount>2){
System.out.println("关闭这个不活跃通道!");
ctx.channel().close();
}
}
}else {
super.userEventTriggered(ctx,evt);
}
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
lossConnectCount = 0;
System.out.println("client says: "+msg.toString());
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}