Parcourir la source

websocket加入考试状态校验

wangliang il y a 4 ans
Parent
commit
4f786064ce

+ 4 - 7
themis-business/src/main/java/com/qmth/themis/business/util/WebsocketUtil.java

@@ -52,9 +52,8 @@ public class WebsocketUtil {
      * 检查考试状态
      *
      * @param recordId
-     * @return
      */
-    public static Boolean checkExamStatus(Long recordId) {
+    public static void checkExamStatus(Long recordId) {
         if (Objects.isNull(recordId)) {
             throw new BusinessException(ExceptionResultEnum.RECORD_ID_IS_NULL);
         }
@@ -62,11 +61,9 @@ public class WebsocketUtil {
         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;
+        if (!Objects.equals(ExamRecordStatusEnum.FIRST_PREPARE, examRecordStatusEnum) && !Objects.equals(ExamRecordStatusEnum.RESUME_PREPARE, examRecordStatusEnum)
+                && !Objects.equals(ExamRecordStatusEnum.ANSWERING, examRecordStatusEnum)) {
+            throw new BusinessException("考试状态错误");
         }
     }
 

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

@@ -168,27 +168,25 @@ public class WebSocketMobileServer implements Concurrently {
             log.warn("Authorization faile: deviceId invalid, session deviceId is " + tbSession.getDeviceId());
             throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);
         }
-        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.recordId + "-" + this.source.name())) {
-                webSocketMap.remove(this.recordId + "-" + this.source.name());
-                webSocketMap.put(this.recordId + "-" + this.source.name(), this);
-            } else {
-                webSocketMap.put(this.recordId + "-" + 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);
+        WebsocketUtil.checkExamStatus(this.recordId);
+        this.session = session;
+        session.setMaxIdleTimeout(SystemConstant.WEBSOCKET_MAX_TIME_OUT);
+        this.sessionId = tbSession.getId();
+        if (webSocketMap.containsKey(this.recordId + "-" + this.source.name())) {
+            webSocketMap.remove(this.recordId + "-" + this.source.name());
+            webSocketMap.put(this.recordId + "-" + this.source.name(), this);
+        } else {
+            webSocketMap.put(this.recordId + "-" + 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);
     }
 
     /**

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

@@ -38,7 +38,9 @@ import java.lang.reflect.Method;
 import java.net.InetSocketAddress;
 import java.time.LocalDateTime;
 import java.time.ZoneOffset;
-import java.util.*;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
 import java.util.concurrent.ConcurrentHashMap;
 
 /**
@@ -162,27 +164,25 @@ public class WebSocketOeServer implements Concurrently {
             log.warn("Authorization faile: deviceId invalid, session deviceId is " + tbSession.getDeviceId());
             throw new BusinessException(ExceptionResultEnum.AUTHORIZATION_ERROR);
         }
-        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.recordId)) {
-                webSocketMap.remove(this.recordId);
-                webSocketMap.put(this.recordId, this);
-            } else {
-                webSocketMap.put(this.recordId, 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);
+        WebsocketUtil.checkExamStatus(this.recordId);
+        this.session = session;
+        session.setMaxIdleTimeout(SystemConstant.WEBSOCKET_MAX_TIME_OUT);
+        this.sessionId = tbSession.getId();
+        if (webSocketMap.containsKey(this.recordId)) {
+            webSocketMap.remove(this.recordId);
+            webSocketMap.put(this.recordId, this);
+        } else {
+            webSocketMap.put(this.recordId, 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);
     }
 
     /**
@@ -346,7 +346,7 @@ public class WebSocketOeServer implements Concurrently {
 
     @Override
     public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,
-            ConsumeConcurrentlyContext consumeConcurrentlyContext) {
+                                                    ConsumeConcurrentlyContext consumeConcurrentlyContext) {
         RedisUtil redisUtil = SpringContextHolder.getBean(RedisUtil.class);
         MqOeLogicService mqOeLogicService = SpringContextHolder.getBean(MqOeLogicService.class);
         MqDto mqDto = null;