|
@@ -1,61 +1,50 @@
|
|
package com.gzlh.background.client;
|
|
package com.gzlh.background.client;
|
|
|
|
|
|
-import com.gzlh.background.client.CustomHeartbeatHandler;
|
|
|
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
|
+import com.gzlh.background.properties.BackgroundPropertiesConfig;
|
|
|
|
+import com.gzlh.event.EventBus;
|
|
|
|
+import com.gzlh.utils.DeviceCache;
|
|
import io.netty.channel.ChannelHandler;
|
|
import io.netty.channel.ChannelHandler;
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
|
+import io.netty.channel.SimpleChannelInboundHandler;
|
|
import io.netty.handler.timeout.IdleStateEvent;
|
|
import io.netty.handler.timeout.IdleStateEvent;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
@ChannelHandler.Sharable
|
|
@ChannelHandler.Sharable
|
|
-public class BackgroundClientHandler extends CustomHeartbeatHandler {
|
|
|
|
|
|
+public class BackgroundClientHandler extends SimpleChannelInboundHandler<String> {
|
|
|
|
+
|
|
private BackgroundClientNetty client;
|
|
private BackgroundClientNetty client;
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
+ private EventBus eventBus;
|
|
|
|
+
|
|
|
|
+
|
|
public BackgroundClientHandler(BackgroundClientNetty client) {
|
|
public BackgroundClientHandler(BackgroundClientNetty client) {
|
|
- super("client");
|
|
|
|
this.client = client;
|
|
this.client = client;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
-// @Override
|
|
|
|
-// protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
|
|
|
|
-// System.out.println(1111);
|
|
|
|
-// System.out.println("Client received: " + msg);
|
|
|
|
-//
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- // @Override
|
|
|
|
-// public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
|
|
|
-// System.out.println(1111);
|
|
|
|
-// System.out.println(msg);
|
|
|
|
-//// ByteBuf in = (ByteBuf) msg;
|
|
|
|
-//// System.out.println("Client received: " + in.toString(CharsetUtil.UTF_8));
|
|
|
|
-//
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
@Override
|
|
@Override
|
|
- protected void handleData(ChannelHandlerContext channelHandlerContext, String msg) {
|
|
|
|
-// byte[] data = new byte[byteBuf.readableBytes() - 5];
|
|
|
|
-// byteBuf.skipBytes(5);
|
|
|
|
-// byteBuf.readBytes(data);
|
|
|
|
-// String content = new String(data);
|
|
|
|
- System.out.println(name + " get content: " + msg);
|
|
|
|
|
|
+ protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
|
|
|
|
+// System.out.println("Client received: " + msg);
|
|
|
|
+ if (!"Heartbeat".equalsIgnoreCase(msg)){
|
|
|
|
+ eventBus.start(msg);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
|
- protected void handleAllIdle(ChannelHandlerContext ctx) {
|
|
|
|
- super.handleAllIdle(ctx);
|
|
|
|
- sendPingMsg(ctx);
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
+ protected void sendHeartbeat(ChannelHandlerContext context) {
|
|
|
|
+// 向后台发送设备信息json字符串
|
|
|
|
+ String jsonStr = JSONUtil.toJsonStr(DeviceCache.getCacheMap());
|
|
|
|
+ context.writeAndFlush(jsonStr);
|
|
|
|
+// System.out.println(" sent ping msg to " + context.channel().remoteAddress());
|
|
|
|
+ }
|
|
|
|
|
|
-// @Override
|
|
|
|
-// public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
|
|
|
-// //当被通知Channel是活跃的时候,发送一条消息
|
|
|
|
-//// ctx.writeAndFlush(Unpooled.copiedBuffer("Netty rocks!", CharsetUtil.UTF_8));
|
|
|
|
-//// sendPingMsg(ctx);
|
|
|
|
-// }
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
|
|
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
|
|
@@ -78,6 +67,21 @@ public class BackgroundClientHandler extends CustomHeartbeatHandler {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ protected void handleReaderIdle(ChannelHandlerContext ctx) {
|
|
|
|
+// System.err.println("---READER_IDLE---");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ protected void handleWriterIdle(ChannelHandlerContext ctx) {
|
|
|
|
+ //写空闲时发送心跳包
|
|
|
|
+ sendHeartbeat(ctx);
|
|
|
|
+// System.err.println("---WRITER_IDLE---");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ protected void handleAllIdle(ChannelHandlerContext ctx) {
|
|
|
|
+ System.err.println("---ALL_IDLE---");
|
|
|
|
+// sendHeartbeat(ctx);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
@@ -87,7 +91,8 @@ public class BackgroundClientHandler extends CustomHeartbeatHandler {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
|
- super.channelInactive(ctx);
|
|
|
|
|
|
+ log.error("-------------后端服务器连接断开,{}-----------,进行重连", ctx.channel().remoteAddress());
|
|
|
|
+// System.err.println("---后端服务器连接断开," + ctx.channel().remoteAddress() + " ---");
|
|
client.connect();
|
|
client.connect();
|
|
}
|
|
}
|
|
|
|
|