|
@@ -1,7 +1,9 @@
|
|
|
package com.persagy.ztkedgeclouddatasecurity.netty.edge;
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.persagy.ztkedgeclouddatasecurity.entity.ChattingUser;
|
|
|
+import com.persagy.ztkedgeclouddatasecurity.entity.NettyMessage;
|
|
|
import com.persagy.ztkedgeclouddatasecurity.kafka.EdgeKafkaProducer;
|
|
|
import com.persagy.ztkedgeclouddatasecurity.netty.MyChannelInitializer;
|
|
|
import io.netty.bootstrap.ServerBootstrap;
|
|
@@ -26,6 +28,8 @@ import java.util.concurrent.ConcurrentHashMap;
|
|
|
public class NettyServer {
|
|
|
|
|
|
public static Map<String, Set<Channel>> socketChannelMap = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+
|
|
|
private Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
@Value("${listener.port}")
|
|
|
private String listenerPort;
|
|
@@ -85,8 +89,8 @@ public class NettyServer {
|
|
|
chattingUser.setIp(ipPortArr[i - 1].split(":")[2]);
|
|
|
ip = ipPortArr[i - 1].split(":")[2];
|
|
|
}
|
|
|
-
|
|
|
- if ("zkt-proj-alarm".equals(ipPortArr[i - 1].split(":")[1])) {
|
|
|
+ //zkt-proj-alarm projectid=Pj4403050019
|
|
|
+ if ("Pj4403050019".equals(ipPortArr[i - 1].split(":")[1])) {
|
|
|
ServerBootstrap serverBootstrap = new ServerBootstrap();
|
|
|
EventLoopGroup bossGroup = new NioEventLoopGroup();
|
|
|
NioEventLoopGroup workerGroup = new NioEventLoopGroup();
|
|
@@ -96,7 +100,7 @@ public class NettyServer {
|
|
|
int port = Integer.valueOf(ipPortArr[i - 1].split(":")[3]);
|
|
|
chattingUser.setPort(port);
|
|
|
list.add(chattingUser);
|
|
|
- serverBootstrap.childHandler(new MyChannelInitializer(chattingUser, edgeKafkaProducer));
|
|
|
+ serverBootstrap.childHandler(new MyChannelInitializer(chattingUser, edgeKafkaProducer,socketChannelMap));
|
|
|
ChannelFuture channelFuture = serverBootstrap.bind(port);
|
|
|
if (ChannelFutures == null) {
|
|
|
ChannelFutures = new ChannelFuture[8];
|
|
@@ -115,4 +119,42 @@ public class NettyServer {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ public static synchronized void addChannel(String projectId, ChannelHandlerContext ctx) {
|
|
|
+ //200 建立和客户端的连接
|
|
|
+ log.warn("添加channel[{}],项目id[{}]", ctx.channel().remoteAddress().toString(), projectId);
|
|
|
+ Set<Channel> channels = CollectionUtil.isEmpty(socketChannelMap.get(projectId)) ? new HashSet<>() : socketChannelMap.get(projectId);
|
|
|
+ channels.add(ctx.channel());
|
|
|
+ socketChannelMap.put(projectId, channels);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean sendMessage(String projectId, NettyMessage msg) {
|
|
|
+ if (socketChannelMap.containsKey(projectId)) {
|
|
|
+ Set<Channel> channels = socketChannelMap.get(projectId);
|
|
|
+ if (CollectionUtil.isNotEmpty(channels)) {
|
|
|
+ for (Channel channel : channels) {
|
|
|
+ if (channel.isActive() && channel.isWritable()) {
|
|
|
+ channel.writeAndFlush(JSONObject.parseObject(msg.toString(), NettyMessage.class));
|
|
|
+ log.info("netty 向边缘端[{}][{}]发送消息:{}", channel.remoteAddress().toString(), projectId, msg);
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ log.error("netty not writable now, message dropped");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.error("projectId[{}]channels获取失败", projectId);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.info("...projectId[{}]未建立连接,消息不发送!", projectId);
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|