deason 3 жил өмнө
parent
commit
86162e7b0d

+ 7 - 3
src/main/java/cn/com/qmth/examcloud/ws/api/provider/WsCloudServiceProvider.java

@@ -17,6 +17,8 @@ import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.logging.log4j.ThreadContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -31,11 +33,12 @@ public class WsCloudServiceProvider implements WsCloudService {
 
     private static final long serialVersionUID = 4784116669909937047L;
 
+    private static final Logger log = LoggerFactory.getLogger(WsCloudServiceProvider.class);
+
     @ApiOperation(value = "发送消息")
     @PostMapping("sendText")
     @Override
     public SendTextResp sendText(@RequestBody SendTextReq req) {
-
         Long rootOrgId = req.getRootOrgId();
         UserType userType = req.getUserType();
         Long userId = req.getUserId();
@@ -50,14 +53,14 @@ public class WsCloudServiceProvider implements WsCloudService {
         String key = user.buildKey();
 
         Session session = WebSocketHelper.getSession(path, key);
-
         if (null == session) {
+            log.error("NO_WS_SESSION key={} path={}", key, path);
             throw new StatusException("100001", "no ws session about path [" + path + "]");
         }
 
         SessionInfo sessionInfo = WebSocketHelper.getSessionInfo(session);
-
         if (null == sessionInfo) {
+            log.error("NO_SESSION_INFO key={} path={}", key, path);
             throw new StatusException("100002", "no ws session info");
         }
 
@@ -69,6 +72,7 @@ public class WsCloudServiceProvider implements WsCloudService {
             try {
                 jsonEl = JsonParser.parseString(content);
             } catch (JsonSyntaxException e) {
+                log.error("WS_MESSAGE_INVALID key={} path={}", key, path);
                 throw new StatusException("100003", "message is not a json string");
             }
         }

+ 3 - 1
src/main/java/cn/com/qmth/examcloud/ws/starter/core/WebSocketHelper.java

@@ -52,9 +52,9 @@ public class WebSocketHelper {
         si.setIndex(index);
         si.setUser(user);
         si.setSessionId(sessionId);
-
         SESESION_INFO_MAP.put(session, si);
 
+        LOG.info("WS_SESSION_IN key={} cacheSize={}", index, INDEX_MAP.size());
     }
 
     /**
@@ -68,6 +68,8 @@ public class WebSocketHelper {
 
         if (null != si) {
             INDEX_MAP.remove(si.getIndex());
+
+            LOG.info("WS_SESSION_OUT key={} cacheSize={}", si.getIndex(), INDEX_MAP.size());
         }
 
         SESESION_INFO_MAP.remove(session);

+ 20 - 12
src/main/java/cn/com/qmth/examcloud/ws/starter/core/WebSocketServerEndpoint.java

@@ -56,13 +56,11 @@ public class WebSocketServerEndpoint {
     @OnOpen
     public void onOpen(Session session, @PathParam("path") String path) {
         long amount = counter.incrementAndGet();
-
         this.sessionId = UUID.randomUUID().toString().replace("-", "");
-
         ThreadContext.put("TRACE_ID", sessionId);
 
-        if (LOG.isDebugEnabled()) {
-            LOG.debug("[onOpen]. path=" + path + "; sessionsAmount=" + amount);
+        if (LOG.isInfoEnabled()) {
+            LOG.info("[onOpen] into... path={} curSessionSize={}", path, amount);
         }
 
         MessageOut out = new MessageOut(path, sessionId);
@@ -106,6 +104,10 @@ public class WebSocketServerEndpoint {
 
         ThreadContext.put("CALLER", user.getKey());
 
+        if (LOG.isInfoEnabled()) {
+            LOG.info("[onOpen] sendText... path={} curSessionSize={} user={}", path, amount, user.getKey());
+        }
+
         WebSocketHelper.sendText(session, path, out);
 
         WebSocketHelper.setSession(path, user, session, sessionId);
@@ -132,18 +134,22 @@ public class WebSocketServerEndpoint {
 
     @OnClose
     public void onClose(Session session, @PathParam("path") String path) {
-
         long amount = counter.decrementAndGet();
 
+        String user = "";
         SessionInfo sessionInfo = WebSocketHelper.getSessionInfo(session);
         if (null != sessionInfo) {
+            user = sessionInfo.getUser().getKey();
             ThreadContext.put("TRACE_ID", sessionInfo.getSessionId());
-            ThreadContext.put("CALLER", sessionInfo.getUser().getKey());
+            ThreadContext.put("CALLER", user);
         } else {
             ThreadContext.put("TRACE_ID", this.sessionId);
         }
 
-        LOG.debug("[onClose]. path=" + path + "; sessionsAmount=" + amount);
+        if (LOG.isInfoEnabled()) {
+            LOG.info("[onClose] path={} curSessionSize={} user={}", path, amount, user);
+        }
+
         WebSocketHelper.closeSession(session);
     }
 
@@ -165,8 +171,8 @@ public class WebSocketServerEndpoint {
             return;
         }
 
-        if (LOG.isDebugEnabled()) {
-            LOG.debug("[onMessage]. path=" + path + ". message=" + message);
+        if (LOG.isInfoEnabled()) {
+            LOG.info("[onMessage] path={} user={}", path, sessionInfo.getUser().getKey());
         }
 
         SessionInfo si = WebSocketHelper.getSessionInfo(session);
@@ -242,16 +248,18 @@ public class WebSocketServerEndpoint {
 
     @OnError
     public void onError(Session session, @PathParam("path") String path, Throwable t) {
-
+        String user = "";
         SessionInfo sessionInfo = WebSocketHelper.getSessionInfo(session);
         if (null != sessionInfo) {
+            user = sessionInfo.getUser().getKey();
             ThreadContext.put("TRACE_ID", sessionInfo.getSessionId());
-            ThreadContext.put("CALLER", sessionInfo.getUser().getKey());
+            ThreadContext.put("CALLER", user);
         } else {
             ThreadContext.put("TRACE_ID", this.sessionId);
         }
 
-        LOG.error("[onError]. path=" + path, t);
+        LOG.error("[onError] path={} user={} ", path, user, t);
+
         WebSocketHelper.closeSession(session);
     }