Browse Source

websocket加入考试状态校验

wangliang 4 years ago
parent
commit
08ee7ab1cd

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

@@ -55,9 +55,8 @@ public class WebsocketUtil {
      * 检查考试状态
      * 检查考试状态
      *
      *
      * @param recordId
      * @param recordId
-     * @return
      */
      */
-    public static Boolean checkExamStatus(Long recordId) {
+    public static void checkExamStatus(Long recordId) {
         if (Objects.isNull(recordId)) {
         if (Objects.isNull(recordId)) {
             throw new BusinessException(ExceptionResultEnum.RECORD_ID_IS_NULL);
             throw new BusinessException(ExceptionResultEnum.RECORD_ID_IS_NULL);
         }
         }
@@ -65,11 +64,9 @@ public class WebsocketUtil {
         if (Objects.isNull(examRecordStatusEnum)) {
         if (Objects.isNull(examRecordStatusEnum)) {
             throw new BusinessException(ExceptionResultEnum.EXAM_STATUS_NOT_NULL);
             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("考试状态错误");
         }
         }
     }
     }
 
 

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

@@ -92,26 +92,24 @@ public class WebSocketMobileServer implements Concurrently {
 
 
         redisUtil = SpringContextHolder.getBean(RedisUtil.class);
         redisUtil = SpringContextHolder.getBean(RedisUtil.class);
         TBSession tbSession = AuthUtil.websocketAuthInterceptor(Platform.valueOf(platform), deviceId, authorization, String.valueOf(mapParameter.get("time").get(0)), SystemConstant.GET, url);
         TBSession tbSession = AuthUtil.websocketAuthInterceptor(Platform.valueOf(platform), deviceId, authorization, String.valueOf(mapParameter.get("time").get(0)), SystemConstant.GET, url);
-        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);
+        WebsocketUtil.checkExamStatus(this.recordId);
+        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();
 //            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, 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);
     }
     }
 
 
     /**
     /**

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

@@ -90,26 +90,24 @@ public class WebSocketOeServer implements Concurrently {
         redisUtil = SpringContextHolder.getBean(RedisUtil.class);
         redisUtil = SpringContextHolder.getBean(RedisUtil.class);
 
 
         TBSession tbSession = AuthUtil.websocketAuthInterceptor(Platform.valueOf(platform), deviceId, authorization, String.valueOf(mapParameter.get("time").get(0)), SystemConstant.GET, url);
         TBSession tbSession = AuthUtil.websocketAuthInterceptor(Platform.valueOf(platform), deviceId, authorization, String.valueOf(mapParameter.get("time").get(0)), SystemConstant.GET, url);
-        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);
+        WebsocketUtil.checkExamStatus(this.recordId);
+        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);
     }
     }
 
 
     /**
     /**