|
@@ -0,0 +1,249 @@
|
|
|
|
+package cn.com.qmth.examcloud.ws.core;
|
|
|
|
+
|
|
|
|
+import java.lang.annotation.Annotation;
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+import javax.websocket.OnClose;
|
|
|
|
+import javax.websocket.OnError;
|
|
|
|
+import javax.websocket.OnMessage;
|
|
|
|
+import javax.websocket.OnOpen;
|
|
|
|
+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.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;
|
|
|
|
+import cn.com.qmth.examcloud.web.redis.RedisClient;
|
|
|
|
+import cn.com.qmth.examcloud.web.support.SpringContextHolder;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 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_LOGGER");
|
|
|
|
+
|
|
|
|
+ private static RedisClient redisClient;
|
|
|
|
+
|
|
|
|
+ private static RedisClient getRedisClient() {
|
|
|
|
+ if (null != redisClient) {
|
|
|
|
+ return redisClient;
|
|
|
|
+ }
|
|
|
|
+ redisClient = SpringContextHolder.getBean(RedisClient.class);
|
|
|
|
+ return redisClient;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @OnOpen
|
|
|
|
+ public void onOpen(Session session, @PathParam("path") String path) {
|
|
|
|
+
|
|
|
|
+ if (WS_LOG.isDebugEnabled()) {
|
|
|
|
+ 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");
|
|
|
|
+
|
|
|
|
+ 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 = getRedisClient().get(key, User.class);
|
|
|
|
+
|
|
|
|
+ if (null == user) {
|
|
|
|
+ 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.setStatus(403);
|
|
|
|
+ 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 {
|
|
|
|
+ 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;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取请求参数
|
|
|
|
+ *
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ * @param session
|
|
|
|
+ * @param key
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private String getRequestParameter(Session session, String key) {
|
|
|
|
+ Map<String, List<String>> params = session.getRequestParameterMap();
|
|
|
|
+ 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) {
|
|
|
|
+ WS_LOG.debug("[onClose]. path=" + path);
|
|
|
|
+ SessionHolder.delSession(session);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @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 = getRedisClient().get(key, User.class);
|
|
|
|
+
|
|
|
|
+ MessageOut out = new MessageOut();
|
|
|
|
+ out.setStatus(200);
|
|
|
|
+ out.setPath(path);
|
|
|
|
+
|
|
|
|
+ if (null == user) {
|
|
|
|
+ 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.setStatus(403);
|
|
|
|
+ out.setStatusCode("403");
|
|
|
|
+ out.setStatusDesc("token is wrong");
|
|
|
|
+ sendText(session, path, out);
|
|
|
|
+ IOUtils.closeQuietly(session);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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("系统异常");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ sendText(session, path, out);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @OnError
|
|
|
|
+ public void onError(Session session, @PathParam("path") String path, Throwable t) {
|
|
|
|
+ WS_LOG.error("[onError]. path=" + path, t);
|
|
|
|
+ SessionHolder.delSession(session);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|