From 97f4845952c1e42804ecca481d0cf109c6369c1f Mon Sep 17 00:00:00 2001
From: puzhibing <393733352@qq.com>
Date: 星期五, 07 四月 2023 17:39:48 +0800
Subject: [PATCH] 修改配置和长连接bug

---
 zuul/src/main/java/com/sinata/zuul/util/echo/NettyChannelMap.java             |    5 ++
 zuul/src/main/java/com/sinata/zuul/controller/NettyController.java            |    7 ---
 zuul/src/main/java/com/sinata/zuul/util/echo/NettyServerController.java       |   62 +++++++++++-------------------
 zuul/src/main/java/com/sinata/zuul/util/applets/NettyWebSocketController.java |   21 ++++------
 zuul/src/main/resources/redis.properties                                      |   13 ++++--
 zuul/src/main/resources/application.yml                                       |    6 --
 6 files changed, 46 insertions(+), 68 deletions(-)

diff --git a/zuul/src/main/java/com/sinata/zuul/controller/NettyController.java b/zuul/src/main/java/com/sinata/zuul/controller/NettyController.java
index b6d35fe..014a079 100644
--- a/zuul/src/main/java/com/sinata/zuul/controller/NettyController.java
+++ b/zuul/src/main/java/com/sinata/zuul/controller/NettyController.java
@@ -36,13 +36,6 @@
                 NettyServerController.sendMsgToClient(channel, msg);
                 return JSON.toJSONString(ResultUtil.success());
             }
-
-            //智慧屏(设备号)
-            channel = NettyChannelMap.getData(id);
-            if(null != channel){
-                NettyServerController.sendMsgToClient(channel, msg);
-                return JSON.toJSONString(ResultUtil.success());
-            }
             return JSON.toJSONString(ResultUtil.error("推送失败-----用户id=" + id));
 
         }
diff --git a/zuul/src/main/java/com/sinata/zuul/util/applets/NettyWebSocketController.java b/zuul/src/main/java/com/sinata/zuul/util/applets/NettyWebSocketController.java
index ee1364a..b7867cd 100644
--- a/zuul/src/main/java/com/sinata/zuul/util/applets/NettyWebSocketController.java
+++ b/zuul/src/main/java/com/sinata/zuul/util/applets/NettyWebSocketController.java
@@ -83,35 +83,30 @@
                 if (StringUtil.isNotEmpty(userId1)) {
                     //确保账号在单个设备上登录
                     if (StringUtil.isNotEmpty(token)) {
-                        NettyChannelMap.update_(token.substring(0, 23), ctx);//存储单点登录的通道
                         String token_ = redisUtil.getValue("USER_Applets_" + userId1);//获取缓存中最新的数据
                         if (StringUtil.isNotEmpty(token_) && !token.equals(token_)) {//不在同一设备上登录,向其他设备发送数据
+                            ChannelHandlerContext data_ = NettyChannelMap.getData_(token_.substring(0, 23));
                             JSONObject msg_ = new JSONObject();
                             msg_.put("code", 200);
                             msg_.put("msg", "SUCCESS");
                             msg_.put("method", "OFFLINE");
                             msg_.put("data", new Object());
-                            this.sendMsgToClient(ctx, msg_.toJSONString());
-                            TimerTask timerTask = new TimerTask() {
+                            this.sendMsgToClient(data_, msg_.toJSONString());
+                            new Timer().schedule(new TimerTask() {
                                 @Override
                                 public void run() {
-                                    NettyChannelMap.remove_(ctx);
+                                    NettyChannelMap.remove_(data_);
                                 }
-                            };
-                            Timer timer = new Timer();
-                            timer.schedule(timerTask, 3000);
-                            timer.cancel();
+                            }, 5000);
                         }
-                        if (StringUtil.isEmpty(token_)) {//确保登录的时候存储token失败的情况
-                            redisUtil.setStrValue("USER_Applets_" + userId1, token);
-                        }
+                        NettyChannelMap.update_(token.substring(0, 23), ctx);//存储单点登录的通道
+                        NettyChannelMap.update("Applets" + userId1, ctx);
+                        redisUtil.setStrValue("USER_Applets_" + userId1, token);
                     }
 
                     //存储业务使用的通道
                     if (null != ctx && ctx.channel().isActive()) {
                         NettyChannelMap.update("Applets" + userId1, ctx);
-//                    String s = NettyMsg.setMsg(Method.ok, new HashMap<String, Object>());
-//                    ctx.writeAndFlush(Unpooled.copiedBuffer((s).getBytes()));
                     }
                 }
 
diff --git a/zuul/src/main/java/com/sinata/zuul/util/echo/NettyChannelMap.java b/zuul/src/main/java/com/sinata/zuul/util/echo/NettyChannelMap.java
index b27fd8f..db90e58 100644
--- a/zuul/src/main/java/com/sinata/zuul/util/echo/NettyChannelMap.java
+++ b/zuul/src/main/java/com/sinata/zuul/util/echo/NettyChannelMap.java
@@ -104,6 +104,11 @@
 		}
 	}
 
+
+	public static synchronized void remove_(String key) {
+		ctxMap.remove(key);
+	}
+
 	
 	/**
 	 * Remove the data resources.
diff --git a/zuul/src/main/java/com/sinata/zuul/util/echo/NettyServerController.java b/zuul/src/main/java/com/sinata/zuul/util/echo/NettyServerController.java
index 1ebf342..b82f217 100644
--- a/zuul/src/main/java/com/sinata/zuul/util/echo/NettyServerController.java
+++ b/zuul/src/main/java/com/sinata/zuul/util/echo/NettyServerController.java
@@ -106,72 +106,56 @@
                         if(StringUtil.isNotEmpty(token)){
                             String token_ = redisUtil.getValue("USER_APP_"+ userId1);//获取缓存中最新的数据
                             if(StringUtil.isNotEmpty(token_) && !token.equals(token_)){//不在同一设备上登录,向其他设备发送数据
-                                JSONObject msg_ = new JSONObject();
+								ChannelHandlerContext data_ = NettyChannelMap.getData_(token_.substring(0, 23));
+								JSONObject msg_ = new JSONObject();
                                 msg_.put("code", 200);
                                 msg_.put("msg", "SUCCESS");
                                 msg_.put("method", "OFFLINE");
                                 msg_.put("data", new Object());
-								this.sendMsgToClient(ctx, msg_.toJSONString());//给当前通道发送消息
-                                TimerTask timerTask = new TimerTask() {
+								this.sendMsgToClient(data_, msg_.toJSONString());//给当前通道发送消息
+                                new Timer().schedule(new TimerTask() {
                                     @Override
                                     public void run() {
-                                        NettyChannelMap.remove_(ctx);
-										NettyChannelMap.remove(ctx);
+                                        NettyChannelMap.remove_(data_);
                                     }
-                                };
-                                Timer timer = new Timer();
-                                timer.schedule(timerTask, 3000);
-                                timer.cancel();
-                            }else{
-								NettyChannelMap.update_(token.substring(0, 23), ctx);
-								NettyChannelMap.update("USER" + userId1, ctx);
-								String s = NettyMsg.setMsg(Method.ok, new HashMap<String, Object>());
-								ctx.writeAndFlush(Unpooled.copiedBuffer((s).getBytes()));
-							}
-                            if(StringUtil.isEmpty(token_)){//确保登录的时候存储token失败的情况
-                                redisUtil.setStrValue("USER_APP_" + userId1, token);
+                                }, 5000);
                             }
+                            NettyChannelMap.update_(token.substring(0, 23), ctx);
+                            NettyChannelMap.update("USER" + userId1, ctx);
+							redisUtil.setStrValue("USER_APP_" + userId1, token);
                         }
 
+						//存储通讯通道
+						if(null != ctx && ctx.channel().isActive()){
+							NettyChannelMap.update("USER" + userId1, ctx);
+						}
                     }else{
                         //确保账号在单个设备上登录
-						String value = redisUtil.getValue("DEVICE_" + userId1);
-						if(StringUtil.isNotEmpty(token) && StringUtil.isEmpty(device) && StringUtil.isEmpty(value)){//APP端登录的操作
+						if(StringUtil.isNotEmpty(token)){//APP端登录的操作
                             String token_ = redisUtil.getValue("DRIVER_" + userId1);//缓存中拿最新数据
                             if(StringUtil.isNotEmpty(token_) && !token.equals(token_)){//不在同一设备上登录,向当前设备发送数据
-                                JSONObject msg_ = new JSONObject();
+                                ChannelHandlerContext data_ = NettyChannelMap.getData_(token_.substring(0, 23));
+								JSONObject msg_ = new JSONObject();
                                 msg_.put("code", 200);
                                 msg_.put("msg", "SUCCESS");
                                 msg_.put("method", "OFFLINE");
                                 msg_.put("data", new Object());
-								this.sendMsgToClient(ctx, msg_.toJSONString());//给当前通道发送消息
-                                TimerTask timerTask = new TimerTask() {
+								this.sendMsgToClient(data_, msg_.toJSONString());//给当前通道发送消息
+                                new Timer().schedule(new TimerTask() {
                                     @Override
                                     public void run() {
-                                        NettyChannelMap.remove_(ctx);
-										NettyChannelMap.remove(ctx);
+                                        NettyChannelMap.remove_(data_);
                                     }
-                                };
-                                Timer timer = new Timer();
-                                timer.schedule(timerTask, 3000);
-                                timer.cancel();
-                            }else{
-								NettyChannelMap.update("DRIVER" + userId1, ctx);
-								NettyChannelMap.update_(token.substring(0, 23), ctx);
-								String s = NettyMsg.setMsg(Method.ok, new HashMap<String, Object>());
-								ctx.writeAndFlush(Unpooled.copiedBuffer((s).getBytes()));
-							}
-                            if(StringUtil.isEmpty(token_)){//确保登录的时候存储token失败的情况
-                                redisUtil.setStrValue("DRIVER_" + userId1, token);
+                                }, 5000);
                             }
+                            NettyChannelMap.update("DRIVER" + userId1, ctx);
+                            NettyChannelMap.update_(token.substring(0, 23), ctx);
+							redisUtil.setStrValue("DRIVER_" + userId1, token);
                         }
-
 
                         //存储通讯通道
                         if(null != ctx && ctx.channel().isActive()){
                             NettyChannelMap.update("DRIVER" + userId1, ctx);
-                            String s = NettyMsg.setMsg(Method.ok, new HashMap<String, Object>());
-                            ctx.writeAndFlush(Unpooled.copiedBuffer((s).getBytes()));
                         }
                     }
                 }
diff --git a/zuul/src/main/resources/application.yml b/zuul/src/main/resources/application.yml
index 206bc1d..b14faa5 100644
--- a/zuul/src/main/resources/application.yml
+++ b/zuul/src/main/resources/application.yml
@@ -1,5 +1,5 @@
 server:
-  port: 80
+  port: 81
 
 spring:
   application:
@@ -24,10 +24,6 @@
       path: /driver/** #配置请求URL的请求规则
       url: http://127.0.0.1:8007 #真正的微服务地址,path匹配的请求都转发到这里
       serviceid: driver-server #指定Eureka注册中心的服务id
-    dispatch-server: #路由调度相关请求
-      path: /dispatch/** #配置请求URL的请求规则
-      url: http://127.0.0.1:8008 #真正的微服务地址,path匹配的请求都转发到这里
-      serviceid: dispatch-server #指定Eureka注册中心的服务id
   #  配置zuul超时时间
   host:
     connect-timeout-millis: 150000
diff --git a/zuul/src/main/resources/redis.properties b/zuul/src/main/resources/redis.properties
index 7cb4d29..5d9655f 100644
--- a/zuul/src/main/resources/redis.properties
+++ b/zuul/src/main/resources/redis.properties
@@ -4,11 +4,16 @@
 # Redis��������ַ
 spring.redis.host=127.0.0.1
 # Redis���������Ӷ˿�
-#spring.redis.port=16379
-spring.redis.port=6379
+spring.redis.port=16379
 # Redis�������������루Ĭ��Ϊ�գ�
-#spring.redis.password=mPMHThYzlT8DWgl8HLqwPEyPOiHDPPB5
-spring.redis.password=123456
+spring.redis.password=cKsEeyffDXG5PzNg8CIbrWxFluXrCprZ
+
+## Redis��������ַ
+#spring.redis.host=127.0.0.1
+## Redis���������Ӷ˿�
+#spring.redis.port=6379
+## Redis�������������루Ĭ��Ϊ�գ�
+#spring.redis.password=123456
 # ���ӳ������������ʹ�ø�ֵ��ʾû�����ƣ�
 spring.redis.jedis.pool.max-active=1024
 # ���ӳ���������ȴ�ʱ�䣨ʹ�ø�ֵ��ʾû�����ƣ�

--
Gitblit v1.7.1