wangwei 5 жил өмнө
parent
commit
fc04b42856

+ 61 - 11
src/main/java/cn/com/qmth/examcloud/ws/core/SessionHolder.java

@@ -4,6 +4,8 @@ import java.util.Map;
 
 import javax.websocket.Session;
 
+import org.apache.commons.io.IOUtils;
+
 import com.google.common.collect.Maps;
 
 /**
@@ -15,30 +17,78 @@ import com.google.common.collect.Maps;
  */
 public class SessionHolder {
 
-	private static Map<String, Session> IdSesesionMap = Maps.newConcurrentMap();
+	private static final Map<String, Session> ID_SESESION_MAP = Maps.newConcurrentMap();
 
-	private static Map<Session, String> sesesionIdMap = Maps.newConcurrentMap();
+	private static final Map<Session, String> SESESION_ID_MAP = Maps.newConcurrentMap();
 
+	/**
+	 * 获取 Session
+	 *
+	 * @author WANGWEI
+	 * @param sessionId
+	 * @return
+	 */
 	public static Session getSession(String sessionId) {
-		Session session = IdSesesionMap.get(sessionId);
+		Session session = ID_SESESION_MAP.get(sessionId);
 		return session;
 	}
 
+	/**
+	 * 获取 sessionId
+	 *
+	 * @author WANGWEI
+	 * @param session
+	 * @return
+	 */
+	public static String getSessionId(Session session) {
+		return SESESION_ID_MAP.get(session);
+	}
+
+	/**
+	 * 重新设置session
+	 *
+	 * @author WANGWEI
+	 * @param sessionId
+	 * @param session
+	 */
 	public static void setSession(String sessionId, Session session) {
-		IdSesesionMap.put(sessionId, session);
+		Session oldSession = ID_SESESION_MAP.get(sessionId);
+		if (null != oldSession && !oldSession.equals(session)) {
+			IOUtils.closeQuietly(oldSession);
+			SESESION_ID_MAP.remove(oldSession);
+		}
+
+		ID_SESESION_MAP.put(sessionId, session);
+		SESESION_ID_MAP.put(session, sessionId);
 	}
 
+	/**
+	 * 删除 session
+	 *
+	 * @author WANGWEI
+	 * @param sessionId
+	 */
 	public static void delSession(String sessionId) {
-		Session session = IdSesesionMap.get(sessionId);
-		if (null == session) {
-			return;
+		Session session = ID_SESESION_MAP.get(sessionId);
+		ID_SESESION_MAP.remove(sessionId);
+		if (null != session) {
+			IOUtils.closeQuietly(session);
+			SESESION_ID_MAP.remove(session);
 		}
-		session = null;
-		IdSesesionMap.remove(sessionId);
 	}
 
-	public String getSessionId(Session session) {
-		return sesesionIdMap.get(session);
+	/**
+	 * 删除 session
+	 *
+	 * @author WANGWEI
+	 * @param session
+	 */
+	public static void delSession(Session session) {
+		String sessionId = SESESION_ID_MAP.get(session);
+		ID_SESESION_MAP.remove(sessionId);
+
+		SESESION_ID_MAP.remove(session);
+		IOUtils.closeQuietly(session);
 	}
 
 }

+ 94 - 13
src/main/java/cn/com/qmth/examcloud/ws/core/WebSocketServerEndpoint.java

@@ -12,45 +12,126 @@ import javax.websocket.Session;
 import javax.websocket.server.PathParam;
 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.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
-import cn.com.qmth.examcloud.commons.util.DateUtil;
+import cn.com.qmth.examcloud.api.commons.security.bean.User;
+import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
+import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
+import cn.com.qmth.examcloud.web.redis.RedisClient;
 
+/**
+ * websocket ServerEndpoint
+ *
+ * @author WANGWEI
+ * @date 2019年11月27日
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
+ */
 @ServerEndpoint("/{path}")
 @Component
 public class WebSocketServerEndpoint {
 
+	private static final ExamCloudLog WS_LOG = ExamCloudLogFactory.getLog("WS");
+
+	@Autowired
+	RedisClient redisClient;
+
 	@OnOpen
 	public void onOpen(Session session, @PathParam("path") String path) {
-		System.out.println("onOpen; session=" + session + "; path=" + path);
+
+		if (WS_LOG.isDebugEnabled()) {
+			WS_LOG.debug("[onOpen]. path=" + path + "");
+		}
+
+		String key = getRequestParameter(session, "key");
+		String token = getRequestParameter(session, "token");
+
+		if (StringUtils.isBlank(key)) {
+			WS_LOG.error("[onOpen-FAIL]. path=" + path + ". key is blank");
+			IOUtils.closeQuietly(session);
+			return;
+		}
+		if (StringUtils.isBlank(token)) {
+			WS_LOG.error("[onOpen-FAIL]. path=" + path + ". token is blank");
+			IOUtils.closeQuietly(session);
+			return;
+		}
+
+		User user = redisClient.get(key, User.class);
+
+		if (null == user) {
+			WS_LOG.error("[onOpen-FAIL]. path=" + path + ". no login.");
+			IOUtils.closeQuietly(session);
+			return;
+		} else if (!token.equals(user.getToken())) {
+			WS_LOG.error("[onOpen-FAIL]. path=" + path + ". token is wrong.");
+			IOUtils.closeQuietly(session);
+			return;
+		}
+
 		try {
-			session.getBasicRemote().sendText(DateUtil.chinaNow() + "| fuck");
+			session.getBasicRemote().sendText("Hello");
 		} catch (IOException e) {
-			e.printStackTrace();
+			WS_LOG.error("[onOpen-FAIL]. path=" + path + "", e);
+			IOUtils.closeQuietly(session);
+			return;
 		}
 
+		String sessionId = buildSessionId(user, path);
+
+		SessionHolder.setSession(sessionId, session);
+
+	}
+
+	/**
+	 * 构建 sessionId
+	 *
+	 * @author WANGWEI
+	 * @param user
+	 * @param path
+	 * @return
+	 */
+	private String buildSessionId(User user, String path) {
+		return user.getKey() + ":" + path;
+	}
+
+	/**
+	 * 获取请求参数
+	 *
+	 * @author WANGWEI
+	 * @param session
+	 * @param key
+	 * @return
+	 */
+	private String getRequestParameter(Session session, String key) {
 		Map<String, List<String>> params = session.getRequestParameterMap();
-		String key = params.get("key").get(0);
-		String token = params.get("token").get(0);
-		System.out.println("key=" + key);
-		System.out.println("token=" + token);
+		List<String> list = params.get(key);
+
+		if (CollectionUtils.isEmpty(list)) {
+			return null;
+		}
+		String value = StringUtils.join(list, ",");
+		return value;
 	}
 
 	@OnClose
 	public void onClose(Session session, @PathParam("path") String path) {
-		System.out.println("onClose; session=" + session + "; path=" + path);
+		WS_LOG.debug("[onClose]. path=" + path);
+		SessionHolder.delSession(session);
 	}
 
 	@OnMessage
 	public void onMessage(Session session, @PathParam("path") String path, String message) {
-		System.out.println("onMessage; session=" + session + "; path=" + path);
-		System.out.println("onMessage; message=" + message);
+		WS_LOG.debug("[onMessage]. path=" + path + ". message=" + message);
 	}
 
 	@OnError
 	public void onError(Session session, @PathParam("path") String path, Throwable t) {
-		System.out.println("onError; session=" + session + "; path=" + path);
-		t.printStackTrace();
+		WS_LOG.error("[onError]. path=" + path, t);
+		SessionHolder.delSession(session);
 	}
 
 }