Quellcode durchsuchen

websocket日志增加

jingyuanchao vor 1 Jahr
Ursprung
Commit
e794f3834f

+ 8 - 0
project_data/sql/0.1.1/soc/soc.sql

@@ -967,3 +967,11 @@ INSERT INTO  sys_dict_data(`dict_sort`, `dict_label`, `dict_value`, `dict_type`,
 INSERT INTO  sys_dict_data(`dict_sort`, `dict_label`, `dict_value`, `dict_type`, `css_class`, `list_class`, `is_default`, `status`, `create_by`, `create_time`, `update_by`, `update_time`, `remark`) VALUES (1, '在线', '1', 'iot_net_status', NULL, 'default', 'N', '0', '超级管理员', '2024-07-10 15:27:13', '', NULL, NULL);
 INSERT INTO  sys_dict_data(`dict_sort`, `dict_label`, `dict_value`, `dict_type`, `css_class`, `list_class`, `is_default`, `status`, `create_by`, `create_time`, `update_by`, `update_time`, `remark`) VALUES (2, '离线', '2', 'iot_net_status', NULL, 'default', 'N', '0', '超级管理员', '2024-07-10 15:27:23', '', NULL, NULL);
 INSERT INTO  sys_dict_data(`dict_sort`, `dict_label`, `dict_value`, `dict_type`, `css_class`, `list_class`, `is_default`, `status`, `create_by`, `create_time`, `update_by`, `update_time`, `remark`) VALUES (3, '异常', '3', 'iot_net_status', NULL, 'default', 'N', '0', '超级管理员', '2024-07-10 15:27:35', '', NULL, NULL);
+
+-- 新增iot服务链接状态字典
+delete from sys_dict_type where dict_type='iot_connect_status';
+delete from sys_dict_data where dict_type='iot_connect_status';
+INSERT INTO sys_dict_type (dict_name, dict_type, status, create_by, create_time, update_by, update_time, remark) VALUES ('iot服务状态', 'iot_connect_status', '0', '超级管理员', '2024-07-08 15:45:42', '', null, null);
+INSERT INTO sys_dict_data (dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time, update_by, update_time, remark) VALUES (0, '未启用', '0', 'iot_connect_status', null, 'default', 'N', '0', '超级管理员', '2024-07-08 15:46:15', '', null, null);
+INSERT INTO sys_dict_data (dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time, update_by, update_time, remark) VALUES (1, '在线', '1', 'iot_connect_status', null, 'default', 'N', '0', '超级管理员', '2024-07-08 15:46:27', '', null, null);
+INSERT INTO sys_dict_data (dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time, update_by, update_time, remark) VALUES (2, '离线', '2', 'iot_connect_status', null, 'default', 'N', '0', '超级管理员', '2024-07-08 15:46:40', '', null, null);

+ 16 - 5
soc-modules/soc-modules-mediator/src/main/java/com/xunmei/mediator/websocket/holder/WebSocketSessionHolder.java

@@ -1,7 +1,9 @@
 package com.xunmei.mediator.websocket.holder;
 
+import cn.hutool.core.util.ObjectUtil;
 import com.xunmei.common.core.utils.StringUtils;
 import com.xunmei.common.redis.utils.RedisUtils;
+import com.xunmei.system.api.util.LogUtils;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
@@ -31,7 +33,7 @@ public class WebSocketSessionHolder {
 
     public static final String REDIS_TOPIC_WEBSOCKET_TOKEN = "websocket:token_";
 
-    public static String updateToken(WebSocketSession session){
+    public static String updateToken(WebSocketSession session) {
         /**
          * 根据session,获取token
          */
@@ -39,7 +41,7 @@ public class WebSocketSessionHolder {
         for (Map.Entry<String, WebSocketSession> entry : USER_SESSION_MAP.entrySet()) {
             WebSocketSession se = entry.getValue();
             String id = se.getId();
-            if(id.equals(session.getId())){
+            if (id.equals(session.getId())) {
                 RedissonClient client = RedisUtils.getClient();
                 RBucket<Object> bucket = client.getBucket(WebSocketSessionHolder.REDIS_TOPIC_WEBSOCKET_TOKEN + entry.getKey());
                 bucket.expire(Duration.ofMillis(1000 * 60 * 35));
@@ -69,7 +71,7 @@ public class WebSocketSessionHolder {
      * @param sessionKey 要移除的会话键
      */
     public static void removeSession(String sessionKey) {
-        if (StringUtils.isNotEmpty(sessionKey) &&  USER_SESSION_MAP.containsKey(sessionKey)) {
+        if (StringUtils.isNotEmpty(sessionKey) && USER_SESSION_MAP.containsKey(sessionKey)) {
             USER_SESSION_MAP.remove(sessionKey);
             RedisUtils.deleteObject(REDIS_TOPIC_WEBSOCKET_TOKEN + sessionKey);
         }
@@ -109,7 +111,7 @@ public class WebSocketSessionHolder {
         for (Map.Entry<String, WebSocketSession> entry : USER_SESSION_MAP.entrySet()) {
             WebSocketSession se = entry.getValue();
             String id1 = se.getId();
-            if(id.equals(id1)){
+            if (id.equals(id1)) {
                 return entry.getKey();
             }
         }
@@ -118,12 +120,21 @@ public class WebSocketSessionHolder {
 
 
     public static void sendAll(String message) {
+        if (ObjectUtil.isEmpty(USER_SESSION_MAP)) {
+            LogUtils.WEBSOCKET_MSG.error("消息广播失败,没有Iot服务在线,消息内容:{}", message);
+            return;
+        }
         try {
+            int x = 0;
             for (WebSocketSession session : USER_SESSION_MAP.values()) {
                 session.sendMessage(new TextMessage(message));
+                x++;
+                String sessionKey = WebSocketSessionHolder.getSessionKey(session);
+                LogUtils.WEBSOCKET_MSG.info("获取设备基础数据,token:{},发送消息内容:{}", sessionKey, message);
             }
+            LogUtils.WEBSOCKET_MSG.info("消息广播成功,发送消息内容:{},发送消息数量:{}", message, x);
         } catch (IOException e) {
-            log.error("消息广播失败:{}",e);
+            log.error("消息广播失败:{}", e);
         }
     }
 }