wangwei 5 years ago
parent
commit
8b35bc4dff

+ 13 - 3
src/main/java/cn/com/qmth/examcloud/ws/core/MessageOut.java

@@ -15,7 +15,9 @@ public class MessageOut implements JsonSerializable {
 
 	private String path;
 
-	private int statusCode = 200;
+	private int status = 200;
+
+	private String statusCode;
 
 	private String statusDesc;
 
@@ -31,11 +33,19 @@ public class MessageOut implements JsonSerializable {
 		this.path = path;
 	}
 
-	public int getStatusCode() {
+	public int getStatus() {
+		return status;
+	}
+
+	public void setStatus(int status) {
+		this.status = status;
+	}
+
+	public String getStatusCode() {
 		return statusCode;
 	}
 
-	public void setStatusCode(int statusCode) {
+	public void setStatusCode(String statusCode) {
 		this.statusCode = statusCode;
 	}
 

+ 74 - 48
src/main/java/cn/com/qmth/examcloud/ws/core/WebSocketServerEndpoint.java

@@ -1,6 +1,5 @@
 package cn.com.qmth.examcloud.ws.core;
 
-import java.io.IOException;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
 import java.util.List;
@@ -20,6 +19,7 @@ import org.apache.commons.lang.StringUtils;
 import org.springframework.stereotype.Component;
 
 import cn.com.qmth.examcloud.api.commons.security.bean.User;
+import cn.com.qmth.examcloud.commons.exception.StatusException;
 import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
 import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
 import cn.com.qmth.examcloud.commons.util.JsonUtil;
@@ -56,6 +56,20 @@ public class WebSocketServerEndpoint {
 			WS_LOG.debug("[onOpen]. path=" + path + "");
 		}
 
+		MessageOut out = new MessageOut();
+		out.setStatus(200);
+		out.setPath(path);
+
+		Method method = MessageHandlerHolder.getMethod(path);
+		if (null == method) {
+			out.setStatus(500);
+			out.setStatusCode("500");
+			out.setStatusDesc("path is wrong.");
+			sendText(session, path, out);
+			IOUtils.closeQuietly(session);
+			return;
+		}
+
 		String key = getRequestParameter(session, "key");
 		String token = getRequestParameter(session, "token");
 
@@ -72,24 +86,23 @@ public class WebSocketServerEndpoint {
 
 		User user = getRedisClient().get(key, User.class);
 
-		MessageOut out = new MessageOut();
-		out.setPath(path);
-
 		if (null == user) {
-			out.setStatusCode(403);
+			out.setStatus(403);
+			out.setStatusCode("403");
 			out.setStatusDesc("no login");
 			sendText(session, path, out);
 			IOUtils.closeQuietly(session);
 			return;
 		} else if (!token.equals(user.getToken())) {
-			out.setStatusCode(403);
+			out.setStatus(403);
+			out.setStatusCode("403");
 			out.setStatusDesc("token is wrong");
 			sendText(session, path, out);
 			IOUtils.closeQuietly(session);
 			return;
 		}
 
-		out.setStatusCode(200);
+		out.setStatusCode("200");
 		out.setStatusDesc("success");
 		sendText(session, path, out);
 
@@ -151,65 +164,78 @@ public class WebSocketServerEndpoint {
 
 		User user = getRedisClient().get(key, User.class);
 
+		MessageOut out = new MessageOut();
+		out.setStatus(200);
+		out.setPath(path);
+
 		if (null == user) {
-			WS_LOG.error("[onMessage-FAIL]. path=" + path + ". no login.");
+			out.setStatus(403);
+			out.setStatusCode("403");
+			out.setStatusDesc("no login");
+			sendText(session, path, out);
 			IOUtils.closeQuietly(session);
 			return;
 		} else if (!token.equals(user.getToken())) {
-			WS_LOG.error("[onMessage-FAIL]. path=" + path + ". token is wrong.");
+			out.setStatus(403);
+			out.setStatusCode("403");
+			out.setStatusDesc("token is wrong");
+			sendText(session, path, out);
 			IOUtils.closeQuietly(session);
 			return;
 		}
 
-		Method method = MessageHandlerHolder.getMethod(path);
-		Object bean = MessageHandlerHolder.getBean(path);
-
-		Annotation[][] an2 = method.getParameterAnnotations();
-
-		boolean hasMessageParam = false;
-		int messageParamIndex = 0;
-		OUTER : for (int i = 0; i < an2.length; i++) {
-			Annotation[] an1 = an2[i];
-			for (Annotation an : an1) {
-				if (an.annotationType().equals(Message.class)) {
-					hasMessageParam = true;
-					messageParamIndex = i;
-					break OUTER;
+		Object result = null;
+		try {
+
+			Method method = MessageHandlerHolder.getMethod(path);
+			Object bean = MessageHandlerHolder.getBean(path);
+
+			Annotation[][] an2 = method.getParameterAnnotations();
+
+			boolean hasMessageParam = false;
+			int messageParamIndex = 0;
+			OUTER : for (int i = 0; i < an2.length; i++) {
+				Annotation[] an1 = an2[i];
+				for (Annotation an : an1) {
+					if (an.annotationType().equals(Message.class)) {
+						hasMessageParam = true;
+						messageParamIndex = i;
+						break OUTER;
+					}
 				}
 			}
-		}
 
-		Class<?>[] parameterTypes = method.getParameterTypes();
-		Object[] args = new Object[parameterTypes.length];
-		for (int i = 0; i < parameterTypes.length; i++) {
-			Class<?> curType = parameterTypes[i];
-			if (hasMessageParam && i == messageParamIndex) {
-				args[i] = JsonUtil.fromJson(message, curType);
-				continue;
-			}
-			if (curType.equals(User.class)) {
-				args[i] = user;
-				continue;
+			Class<?>[] parameterTypes = method.getParameterTypes();
+			Object[] args = new Object[parameterTypes.length];
+			for (int i = 0; i < parameterTypes.length; i++) {
+				Class<?> curType = parameterTypes[i];
+				if (hasMessageParam && i == messageParamIndex) {
+					args[i] = JsonUtil.fromJson(message, curType);
+					continue;
+				}
+				if (curType.equals(User.class)) {
+					args[i] = user;
+					continue;
+				}
 			}
-		}
 
-		Object result = null;
-		try {
 			result = method.invoke(bean, args);
+			out.setStatusCode("200");
+			out.setStatusDesc("success");
+			out.setContent(result);
+		} catch (StatusException e) {
+			WS_LOG.error("[onMessage-FAIL]. path=" + path + "", e);
+			out.setStatus(500);
+			out.setStatusCode(e.getCode());
+			out.setStatusDesc(e.getDesc());
 		} catch (Exception e) {
 			WS_LOG.error("[onMessage-FAIL]. path=" + path + "", e);
+			out.setStatus(500);
+			out.setStatusCode("500");
+			out.setStatusDesc("系统异常");
 		}
 
-		if (null != result) {
-			try {
-				session.getBasicRemote().sendText(JsonUtil.toJson(result));
-			} catch (IOException e) {
-				WS_LOG.error("[onMessage-FAIL]. path=" + path + "", e);
-				IOUtils.closeQuietly(session);
-				return;
-			}
-		}
-
+		sendText(session, path, out);
 	}
 
 	@OnError