wangwei 5 năm trước cách đây
mục cha
commit
007aac7ae4

+ 25 - 5
src/main/java/cn/com/qmth/examcloud/ws/core/MessageOut.java

@@ -13,18 +13,38 @@ public class MessageOut implements JsonSerializable {
 
 	private static final long serialVersionUID = -2255327250550304236L;
 
-	private String eventType;
+	private String path;
+
+	private int statusCode = 200;
+
+	private String statusDesc;
 
 	private String eventId;
 
 	private Object content;
 
-	public String getEventType() {
-		return eventType;
+	public String getPath() {
+		return path;
+	}
+
+	public void setPath(String path) {
+		this.path = path;
+	}
+
+	public int getStatusCode() {
+		return statusCode;
+	}
+
+	public void setStatusCode(int statusCode) {
+		this.statusCode = statusCode;
+	}
+
+	public String getStatusDesc() {
+		return statusDesc;
 	}
 
-	public void setEventType(String eventType) {
-		this.eventType = eventType;
+	public void setStatusDesc(String statusDesc) {
+		this.statusDesc = statusDesc;
 	}
 
 	public String getEventId() {

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

@@ -59,7 +59,9 @@ public class SessionHolder {
 	 */
 	public static void delSession(Session session) {
 		String sessionId = SESESION_ID_MAP.get(session);
-		ID_SESESION_MAP.remove(sessionId);
+		if (null != sessionId) {
+			ID_SESESION_MAP.remove(sessionId);
+		}
 
 		SESESION_ID_MAP.remove(session);
 		SESSION_KEY_MAP.remove(session);

+ 32 - 7
src/main/java/cn/com/qmth/examcloud/ws/core/WebSocketServerEndpoint.java

@@ -72,25 +72,50 @@ public class WebSocketServerEndpoint {
 
 		User user = getRedisClient().get(key, User.class);
 
+		MessageOut out = new MessageOut();
+		out.setPath(path);
+
 		if (null == user) {
-			WS_LOG.error("[onOpen-FAIL]. path=" + path + ". no login.");
+			out.setStatusCode(403);
+			out.setStatusDesc("no login");
+			sendText(session, path, out);
 			IOUtils.closeQuietly(session);
 			return;
 		} else if (!token.equals(user.getToken())) {
-			WS_LOG.error("[onOpen-FAIL]. path=" + path + ". token is wrong.");
+			out.setStatusCode(403);
+			out.setStatusDesc("token is wrong");
+			sendText(session, path, out);
 			IOUtils.closeQuietly(session);
 			return;
 		}
 
+		out.setStatusCode(200);
+		out.setStatusDesc("success");
+		sendText(session, path, out);
+
+		SessionHolder.setSession(path, user.getKey(), user.getToken(), session);
+	}
+
+	/**
+	 * 发送消息
+	 *
+	 * @author WANGWEI
+	 * @param session
+	 * @param path
+	 * @param message
+	 */
+	private void sendText(Session session, String path, MessageOut out) {
 		try {
-			session.getBasicRemote().sendText("Hello");
-		} catch (IOException e) {
-			WS_LOG.error("[onOpen-FAIL]. path=" + path + "", e);
+			String message = JsonUtil.toJson(out);
+			if (WS_LOG.isDebugEnabled()) {
+				WS_LOG.error("[sendText]. path=" + path + "; message=" + message);
+			}
+			session.getBasicRemote().sendText(message);
+		} catch (Exception e) {
+			WS_LOG.error("[sendText-FAIL]. path=" + path + "", e);
 			IOUtils.closeQuietly(session);
 			return;
 		}
-
-		SessionHolder.setSession(path, user.getKey(), user.getToken(), session);
 	}
 
 	/**