|
@@ -1,6 +1,8 @@
|
|
|
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;
|
|
|
import java.util.Map;
|
|
|
|
|
@@ -21,6 +23,7 @@ import org.springframework.stereotype.Component;
|
|
|
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.commons.util.JsonUtil;
|
|
|
import cn.com.qmth.examcloud.web.redis.RedisClient;
|
|
|
|
|
|
/**
|
|
@@ -80,22 +83,7 @@ public class WebSocketServerEndpoint {
|
|
|
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;
|
|
|
+ SessionHolder.setSession(path, user.getKey(), user.getToken(), session);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -126,6 +114,70 @@ public class WebSocketServerEndpoint {
|
|
|
@OnMessage
|
|
|
public void onMessage(Session session, @PathParam("path") String path, String message) {
|
|
|
WS_LOG.debug("[onMessage]. path=" + path + ". message=" + message);
|
|
|
+ String key = SessionHolder.getKey(session);
|
|
|
+ String token = SessionHolder.getToken(session);
|
|
|
+
|
|
|
+ User user = redisClient.get(key, User.class);
|
|
|
+
|
|
|
+ if (null == user) {
|
|
|
+ WS_LOG.error("[onMessage-FAIL]. path=" + path + ". no login.");
|
|
|
+ IOUtils.closeQuietly(session);
|
|
|
+ return;
|
|
|
+ } else if (!token.equals(user.getToken())) {
|
|
|
+ WS_LOG.error("[onMessage-FAIL]. path=" + path + ". token is wrong.");
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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);
|
|
|
+ } catch (Exception e) {
|
|
|
+ WS_LOG.error("[onMessage-FAIL]. path=" + path + "", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
@OnError
|