فهرست منبع

websocket加入考试状态校验

wangliang 4 سال پیش
والد
کامیت
a5e2082f84

+ 25 - 0
themis-business/src/main/java/com/qmth/themis/business/util/WebsocketUtil.java

@@ -3,8 +3,11 @@ package com.qmth.themis.business.util;
 import com.qmth.themis.business.cache.ExamRecordCacheUtil;
 import com.qmth.themis.business.constant.SpringContextHolder;
 import com.qmth.themis.business.enums.ExamRecordFieldEnum;
+import com.qmth.themis.business.enums.ExamRecordStatusEnum;
 import com.qmth.themis.business.enums.WebsocketStatusEnum;
 import com.qmth.themis.business.service.TOeExamRecordService;
+import com.qmth.themis.common.enums.ExceptionResultEnum;
+import com.qmth.themis.common.exception.BusinessException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -48,6 +51,28 @@ public class WebsocketUtil {
         return null;
     }
 
+    /**
+     * 检查考试状态
+     *
+     * @param recordId
+     * @return
+     */
+    public static Boolean checkExamStatus(Long recordId) {
+        if (Objects.isNull(recordId)) {
+            throw new BusinessException(ExceptionResultEnum.RECORD_ID_IS_NULL);
+        }
+        ExamRecordStatusEnum examRecordStatusEnum = ExamRecordCacheUtil.getStatus(recordId);
+        if (Objects.isNull(examRecordStatusEnum)) {
+            throw new BusinessException(ExceptionResultEnum.EXAM_STATUS_NOT_NULL);
+        }
+        if (ExamRecordStatusEnum.FIRST_PREPARE == examRecordStatusEnum || ExamRecordStatusEnum.RESUME_PREPARE == examRecordStatusEnum
+                || ExamRecordStatusEnum.ANSWERING == examRecordStatusEnum) {
+            return true;
+        } else {
+            return false;
+        }
+    }
+
     /**
      * 更新websocket状态
      *

+ 1 - 1
themis-common/src/main/java/com/qmth/themis/common/enums/ExceptionResultEnum.java

@@ -194,7 +194,7 @@ public enum ExceptionResultEnum {
 
     SOURCE_INVALID(401, 401009, "source无效"),
 
-    RECORD_ID_INVALID(401, 401009, "recordId无效"),
+    RECORD_ID_INVALID(401, 401010, "recordId无效"),
 
     /**
      * 404

+ 18 - 15
themis-exam/src/main/java/com/qmth/themis/exam/websocket/WebSocketMobileServer.java

@@ -92,23 +92,26 @@ public class WebSocketMobileServer implements Concurrently {
 
         redisUtil = SpringContextHolder.getBean(RedisUtil.class);
         TBSession tbSession = AuthUtil.websocketAuthInterceptor(Platform.valueOf(platform), deviceId, authorization, String.valueOf(mapParameter.get("time").get(0)), SystemConstant.GET, url);
-        this.session = session;
-        session.setMaxIdleTimeout(SystemConstant.WEBSOCKET_MAX_TIME_OUT);
-        this.sessionId = tbSession.getId();
-        if (webSocketMap.containsKey(this.session.getId() + "-" + this.source.name())) {
-            webSocketMap.remove(this.session.getId() + "-" + this.source.name());
-            webSocketMap.put(this.session.getId() + "-" + this.source.name(), this);
-        } else {
-            webSocketMap.put(this.session.getId() + "-" + this.source.name(), this);
+        Boolean check = WebsocketUtil.checkExamStatus(this.recordId);
+        if (check) {
+            this.session = session;
+            session.setMaxIdleTimeout(SystemConstant.WEBSOCKET_MAX_TIME_OUT);
+            this.sessionId = tbSession.getId();
+            if (webSocketMap.containsKey(this.session.getId() + "-" + this.source.name())) {
+                webSocketMap.remove(this.session.getId() + "-" + this.source.name());
+                webSocketMap.put(this.session.getId() + "-" + this.source.name(), this);
+            } else {
+                webSocketMap.put(this.session.getId() + "-" + this.source.name(), this);
 //            addOnlineCount();
-        }
+            }
 //        log.info("用户连接:{},当前在线人数为:{}", this.sessionId, getOnlineCount());
-        log.info("用户连接:{}", this.sessionId);
-        InetSocketAddress addr = (InetSocketAddress) WebsocketUtil.getFieldInstance(this.session.getAsyncRemote(), "base#socketWrapper#socket#sc#remoteAddress");
-        this.ip = addr.toString().replace("/", "").split(":")[0];
-        log.info("ip[:{}]连接成功", this.ip);
-        this.updateTime = System.currentTimeMillis();
-        tranMap = WebsocketUtil.initWebsocket(recordId, null, deviceId, ip, updateTime);
+            log.info("用户连接:{}", this.sessionId);
+            InetSocketAddress addr = (InetSocketAddress) WebsocketUtil.getFieldInstance(this.session.getAsyncRemote(), "base#socketWrapper#socket#sc#remoteAddress");
+            this.ip = addr.toString().replace("/", "").split(":")[0];
+            log.info("ip[:{}]连接成功", this.ip);
+            this.updateTime = System.currentTimeMillis();
+            tranMap = WebsocketUtil.initWebsocket(recordId, null, deviceId, ip, updateTime);
+        }
     }
 
     /**

+ 19 - 16
themis-exam/src/main/java/com/qmth/themis/exam/websocket/WebSocketOeServer.java

@@ -90,23 +90,26 @@ public class WebSocketOeServer implements Concurrently {
         redisUtil = SpringContextHolder.getBean(RedisUtil.class);
 
         TBSession tbSession = AuthUtil.websocketAuthInterceptor(Platform.valueOf(platform), deviceId, authorization, String.valueOf(mapParameter.get("time").get(0)), SystemConstant.GET, url);
-        this.session = session;
-        session.setMaxIdleTimeout(SystemConstant.WEBSOCKET_MAX_TIME_OUT);
-        this.sessionId = tbSession.getId();
-        if (webSocketMap.containsKey(this.session.getId())) {
-            webSocketMap.remove(this.session.getId());
-            webSocketMap.put(this.session.getId(), this);
-        } else {
-            webSocketMap.put(this.session.getId(), this);
-            addOnlineCount();
+        Boolean check = WebsocketUtil.checkExamStatus(this.recordId);
+        if (check) {
+            this.session = session;
+            session.setMaxIdleTimeout(SystemConstant.WEBSOCKET_MAX_TIME_OUT);
+            this.sessionId = tbSession.getId();
+            if (webSocketMap.containsKey(this.session.getId())) {
+                webSocketMap.remove(this.session.getId());
+                webSocketMap.put(this.session.getId(), this);
+            } else {
+                webSocketMap.put(this.session.getId(), this);
+                addOnlineCount();
+            }
+            log.info("用户连接:{},当前在线人数为:{}", this.sessionId, getOnlineCount());
+            InetSocketAddress addr = (InetSocketAddress) WebsocketUtil.getFieldInstance(this.session.getAsyncRemote(), "base#socketWrapper#socket#sc#remoteAddress");
+            this.ip = addr.toString().replace("/", "").split(":")[0];
+            log.info("ip[:{}]连接成功", this.ip);
+            WebsocketUtil.updateExamRecordWebsocketStatus(recordId, ip, this.session.getId(), WebsocketStatusEnum.ON_LINE);
+            this.updateTime = System.currentTimeMillis();
+            tranMap = WebsocketUtil.initWebsocket(recordId, null, deviceId, ip, updateTime);
         }
-        log.info("用户连接:{},当前在线人数为:{}", this.sessionId, getOnlineCount());
-        InetSocketAddress addr = (InetSocketAddress) WebsocketUtil.getFieldInstance(this.session.getAsyncRemote(), "base#socketWrapper#socket#sc#remoteAddress");
-        this.ip = addr.toString().replace("/", "").split(":")[0];
-        log.info("ip[:{}]连接成功", this.ip);
-        WebsocketUtil.updateExamRecordWebsocketStatus(recordId, ip, this.session.getId(), WebsocketStatusEnum.ON_LINE);
-        this.updateTime = System.currentTimeMillis();
-        tranMap = WebsocketUtil.initWebsocket(recordId, null, deviceId, ip, updateTime);
     }
 
     /**