wangwei 5 lat temu
rodzic
commit
a4b2c9f0cb

+ 10 - 0
src/main/java/cn/com/qmth/examcloud/ws/starter/core/MessageOut.java

@@ -15,6 +15,8 @@ public class MessageOut implements JsonSerializable {
 
 	private String path;
 
+	private String sessionId;
+
 	private int status = 200;
 
 	private String statusCode;
@@ -33,6 +35,14 @@ public class MessageOut implements JsonSerializable {
 		this.path = path;
 	}
 
+	public String getSessionId() {
+		return sessionId;
+	}
+
+	public void setSessionId(String sessionId) {
+		this.sessionId = sessionId;
+	}
+
 	public int getStatus() {
 		return status;
 	}

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

@@ -17,9 +17,9 @@ import com.google.common.collect.Maps;
  */
 public class SessionHolder {
 
-	private static final Map<String, Session> ID_SESESION_MAP = Maps.newConcurrentMap();
+	private static final Map<String, Session> INDEX_SESESION_MAP = Maps.newConcurrentMap();
 
-	private static final Map<Session, String> SESESION_ID_MAP = Maps.newConcurrentMap();
+	private static final Map<Session, String> SESESION_INDEX_MAP = Maps.newConcurrentMap();
 
 	private static final Map<Session, String> SESSION_KEY_MAP = Maps.newConcurrentMap();
 
@@ -35,17 +35,17 @@ public class SessionHolder {
 	 * @param session
 	 */
 	public static void setSession(String path, String key, String token, Session session) {
-		String sessionId = key + ":" + path;
-		Session oldSession = ID_SESESION_MAP.get(sessionId);
+		String index = key + ":" + path;
+		Session oldSession = INDEX_SESESION_MAP.get(index);
 		if (null != oldSession && !oldSession.equals(session)) {
-			SESESION_ID_MAP.remove(oldSession);
+			SESESION_INDEX_MAP.remove(oldSession);
 			SESSION_KEY_MAP.remove(oldSession);
 			SESSION_TOKEN_MAP.remove(oldSession);
 			IOUtils.closeQuietly(oldSession);
 		}
 
-		ID_SESESION_MAP.put(sessionId, session);
-		SESESION_ID_MAP.put(session, sessionId);
+		INDEX_SESESION_MAP.put(index, session);
+		SESESION_INDEX_MAP.put(session, index);
 
 		SESSION_KEY_MAP.put(session, key);
 		SESSION_TOKEN_MAP.put(session, token);
@@ -58,12 +58,12 @@ public class SessionHolder {
 	 * @param session
 	 */
 	public static void delSession(Session session) {
-		String sessionId = SESESION_ID_MAP.get(session);
+		String sessionId = SESESION_INDEX_MAP.get(session);
 		if (null != sessionId) {
-			ID_SESESION_MAP.remove(sessionId);
+			INDEX_SESESION_MAP.remove(sessionId);
 		}
 
-		SESESION_ID_MAP.remove(session);
+		SESESION_INDEX_MAP.remove(session);
 		SESSION_KEY_MAP.remove(session);
 		SESSION_TOKEN_MAP.remove(session);
 
@@ -79,8 +79,8 @@ public class SessionHolder {
 	 * @return
 	 */
 	public Session getSession(String path, String key) {
-		String sessionId = key + ":" + path;
-		Session session = ID_SESESION_MAP.get(sessionId);
+		String index = key + ":" + path;
+		Session session = INDEX_SESESION_MAP.get(index);
 		return session;
 	}
 

+ 16 - 0
src/main/java/cn/com/qmth/examcloud/ws/starter/core/WebSocketServerEndpoint.java

@@ -16,6 +16,7 @@ import javax.websocket.server.ServerEndpoint;
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.StringUtils;
+import org.apache.logging.log4j.ThreadContext;
 import org.springframework.stereotype.Component;
 
 import cn.com.qmth.examcloud.api.commons.security.bean.User;
@@ -51,12 +52,15 @@ public class WebSocketServerEndpoint {
 
 	@OnOpen
 	public void onOpen(Session session, @PathParam("path") String path) {
+		String sessionId = session.toString().substring(session.getClass().getName().length() + 1);
+		ThreadContext.put("TRACE_ID", sessionId);
 
 		if (WS_LOG.isDebugEnabled()) {
 			WS_LOG.debug("[onOpen]. path=" + path + "");
 		}
 
 		MessageOut out = new MessageOut();
+		out.setSessionId(sessionId);
 		out.setStatus(200);
 		out.setPath(path);
 
@@ -102,6 +106,8 @@ public class WebSocketServerEndpoint {
 			return;
 		}
 
+		ThreadContext.put("CALLER", user.getKey());
+
 		out.setStatusCode("200");
 		out.setStatusDesc("success");
 		sendText(session, path, out);
@@ -152,12 +158,18 @@ public class WebSocketServerEndpoint {
 
 	@OnClose
 	public void onClose(Session session, @PathParam("path") String path) {
+		String sessionId = session.toString().substring(session.getClass().getName().length() + 1);
+		ThreadContext.put("TRACE_ID", sessionId);
+
 		WS_LOG.debug("[onClose]. path=" + path);
 		SessionHolder.delSession(session);
 	}
 
 	@OnMessage
 	public void onMessage(Session session, @PathParam("path") String path, String message) {
+		String sessionId = session.toString().substring(session.getClass().getName().length() + 1);
+		ThreadContext.put("TRACE_ID", sessionId);
+
 		WS_LOG.debug("[onMessage]. path=" + path + ". message=" + message);
 		String key = SessionHolder.getKey(session);
 		String token = SessionHolder.getToken(session);
@@ -165,6 +177,7 @@ public class WebSocketServerEndpoint {
 		User user = getRedisClient().get(key, User.class);
 
 		MessageOut out = new MessageOut();
+		out.setSessionId(sessionId);
 		out.setStatus(200);
 		out.setPath(path);
 
@@ -242,6 +255,9 @@ public class WebSocketServerEndpoint {
 
 	@OnError
 	public void onError(Session session, @PathParam("path") String path, Throwable t) {
+		String sessionId = session.toString().substring(session.getClass().getName().length() + 1);
+		ThreadContext.put("TRACE_ID", sessionId);
+
 		WS_LOG.error("[onError]. path=" + path, t);
 		SessionHolder.delSession(session);
 	}