wangliang 3 gadi atpakaļ
vecāks
revīzija
fb1cc5cd6c

+ 18 - 6
themis-admin/src/main/java/com/qmth/themis/admin/websocket/WebSocketAdminServer.java

@@ -8,12 +8,12 @@ import com.qmth.themis.business.constant.SpringContextHolder;
 import com.qmth.themis.business.constant.SystemConstant;
 import com.qmth.themis.business.dto.WebsocketDto;
 import com.qmth.themis.business.entity.TBSession;
-import com.qmth.themis.business.enums.MonitorVideoSourceEnum;
 import com.qmth.themis.business.enums.WebsocketTypeEnum;
-import com.qmth.themis.business.util.*;
+import com.qmth.themis.business.util.JacksonUtil;
+import com.qmth.themis.business.util.RedisUtil;
+import com.qmth.themis.business.util.UidUtil;
+import com.qmth.themis.business.util.WebsocketUtil;
 import com.qmth.themis.common.contanst.Constants;
-import com.qmth.themis.common.enums.ExceptionResultEnum;
-import com.qmth.themis.common.enums.Platform;
 import com.qmth.themis.common.exception.BusinessException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -24,7 +24,6 @@ import javax.websocket.server.ServerEndpoint;
 import java.io.IOException;
 import java.lang.reflect.Method;
 import java.net.InetSocketAddress;
-import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 import java.util.concurrent.ConcurrentHashMap;
@@ -130,8 +129,9 @@ public class WebSocketAdminServer
      * @param error
      */
     @OnError
-    public void onError(Session session, Throwable error) {
+    public void onError(Session session, Throwable error) throws IOException {
         log.error("用户错误:{},原因:{}", this.websocketSessionId, error);
+        close(this);
         throw new BusinessException(error.getMessage());
     }
 
@@ -236,6 +236,18 @@ public class WebSocketAdminServer
 //        return ConsumeOrderlyStatus.SUCCESS;//成功
 //    }
 
+    /**
+     * 关闭session
+     *
+     * @param webSocketAdminServer
+     */
+    public static void close(WebSocketAdminServer webSocketAdminServer) throws IOException {
+        //判断当前连接是否还在线
+        if (Objects.nonNull(webSocketAdminServer) && Objects.nonNull(webSocketAdminServer.session) && webSocketAdminServer.session.isOpen()) {
+            webSocketAdminServer.session.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, SystemConstant.WEBSOCKET_CLOSE));
+        }
+    }
+
     public static ConcurrentHashMap<Long, WebSocketAdminServer> getWebSocketMap() {
         return webSocketMap;
     }

+ 2 - 0
themis-business/src/main/java/com/qmth/themis/business/constant/SystemConstant.java

@@ -89,6 +89,8 @@ public class SystemConstant {
     /**
      * 系统相关
      */
+    public static final String WEBSOCKET_CLOSE = "服务器端关闭";
+
     public static final String LOG_ERROR = "请求出错:";
 
     public static final String EXTEND_COLUMN = "extendColumn";

+ 7 - 0
themis-exam/src/main/java/com/qmth/themis/exam/api/TEExamController.java

@@ -230,6 +230,13 @@ public class TEExamController {
             throw new BusinessException("时间戳不能为空");
         }
 
+//        Map mqMap = new HashMap<>();
+//        mqMap.put(SystemConstant.RECORD_ID, param.getRecordId());
+//        mqMap.put("source", MonitorVideoSourceEnum.MOBILE_FIRST.name());
+//        //监控结束
+//        MqDto mqDtoStop = new MqDto(mqUtil.getMqGroupDomain().getTopic(), MqTagEnum.MONITOR_STOP.name(), param.getRecordId(), MqTagEnum.MONITOR_STOP, String.valueOf(param.getRecordId()), mqMap, String.valueOf(param.getRecordId()));
+//        mqDtoService.assembleSendOneOrderMsg(mqDtoStop);
+
         AnswerSubmitBean ret = teExamService.answerSubmit(teStudent.getId(), param.getRecordId(), param.getMainNumber(),
                 param.getSubNumber(), param.getSubIndex(), param.getAnswer(), param.getVersion(),
                 param.getDurationSeconds());

+ 3 - 2
themis-exam/src/main/java/com/qmth/themis/exam/listener/service/MqOeLogicService.java

@@ -2,6 +2,7 @@ package com.qmth.themis.exam.listener.service;
 
 import com.qmth.themis.business.dto.MqDto;
 
+import java.io.IOException;
 import java.security.NoSuchAlgorithmException;
 
 /**
@@ -19,7 +20,7 @@ public interface MqOeLogicService {
      * @param mqDto
      * @param key
      */
-    public void execMqOeLogic(MqDto mqDto, String key) throws NoSuchAlgorithmException;
+    public void execMqOeLogic(MqDto mqDto, String key) throws NoSuchAlgorithmException, IOException;
 
     /**
      * oe mobile 逻辑
@@ -27,5 +28,5 @@ public interface MqOeLogicService {
      * @param mqDto
      * @param key
      */
-    public void execMqOeMobileLogic(MqDto mqDto, String key) throws NoSuchAlgorithmException;
+    public void execMqOeMobileLogic(MqDto mqDto, String key) throws NoSuchAlgorithmException, IOException;
 }

+ 14 - 13
themis-exam/src/main/java/com/qmth/themis/exam/listener/service/impl/MqOeLogicServiceImpl.java

@@ -25,6 +25,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
+import java.io.IOException;
 import java.security.NoSuchAlgorithmException;
 import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
@@ -66,7 +67,7 @@ public class MqOeLogicServiceImpl implements MqOeLogicService {
      */
     @Override
     @Transactional
-    public void execMqOeLogic(MqDto mqDto, String key) throws NoSuchAlgorithmException {
+    public void execMqOeLogic(MqDto mqDto, String key) throws NoSuchAlgorithmException, IOException {
         Gson gson = new Gson();
         ConcurrentHashMap<String, WebSocketOeServer> webSocketMap = WebSocketOeServer.getWebSocketMap();
         String tag = mqDto.getTag();
@@ -234,23 +235,23 @@ public class MqOeLogicServiceImpl implements MqOeLogicService {
                                 ConcurrentHashMap<String, WebSocketMobileServer> webSocketMobileMap = WebSocketMobileServer.getWebSocketMap();
 
                                 String mobileWebsocketId = null;
-                                WebsocketStatusEnum websocketStatusEnum = null;
+//                                WebsocketStatusEnum websocketStatusEnum = null;
                                 if (source == MonitorVideoSourceEnum.MOBILE_FIRST) {
                                     mobileWebsocketId = ExamRecordCacheUtil.getMobileFirstWebsocketId(recordId);
-                                    websocketStatusEnum = ExamRecordCacheUtil.getMobileFirstWebsocketStatus(recordId);
+//                                    websocketStatusEnum = ExamRecordCacheUtil.getMobileFirstWebsocketStatus(recordId);
                                 } else if (source == MonitorVideoSourceEnum.MOBILE_SECOND) {
                                     mobileWebsocketId = ExamRecordCacheUtil.getMobileSecondWebsocketId(recordId);
-                                    websocketStatusEnum = ExamRecordCacheUtil.getMobileSecondWebsocketStatus(recordId);
+//                                    websocketStatusEnum = ExamRecordCacheUtil.getMobileSecondWebsocketStatus(recordId);
                                 }
-                                if (Objects.nonNull(websocketStatusEnum) && websocketStatusEnum == WebsocketStatusEnum.ON_LINE) {
-                                    WebSocketMobileServer webSocketMobileServer = webSocketMobileMap.get(mobileWebsocketId + "-" + source.name());
-                                    Long examStudentId = ExamRecordCacheUtil.getExamStudentId(recordId);
-                                    ExamStudentCacheBean examStudentCacheBean = teExamStudentService.getExamStudentCacheBean(examStudentId);
-                                    Optional.ofNullable(examStudentCacheBean).orElseThrow(() -> new BusinessException("考生数据为空"));
+//                                if (Objects.nonNull(websocketStatusEnum) && websocketStatusEnum == WebsocketStatusEnum.ON_LINE) {
+                                WebSocketMobileServer webSocketMobileServer = webSocketMobileMap.get(mobileWebsocketId + "-" + source.name());
+                                Long examStudentId = ExamRecordCacheUtil.getExamStudentId(recordId);
+                                ExamStudentCacheBean examStudentCacheBean = teExamStudentService.getExamStudentCacheBean(examStudentId);
+                                Optional.ofNullable(examStudentCacheBean).orElseThrow(() -> new BusinessException("考生数据为空"));
 
-                                    SystemConstant.mobileMonitorStatusStop(examStudentCacheBean.getStudentId(), recordId, true);
-                                    WebSocketMobileServer.close(webSocketMobileServer);
-                                }
+                                SystemConstant.mobileMonitorStatusStop(examStudentCacheBean.getStudentId(), recordId, true);
+                                WebSocketMobileServer.close(webSocketMobileServer);
+//                                }
                             }
                             break;
                         case "OE_WEBSOCKET_MOBILE_MONITOR_STATUS":
@@ -291,7 +292,7 @@ public class MqOeLogicServiceImpl implements MqOeLogicService {
      */
     @Override
     @Transactional
-    public void execMqOeMobileLogic(MqDto mqDto, String key) throws NoSuchAlgorithmException {
+    public void execMqOeMobileLogic(MqDto mqDto, String key) throws NoSuchAlgorithmException, IOException {
         Gson gson = new Gson();
         String tag = mqDto.getTag();
         ConcurrentHashMap<String, WebSocketMobileServer> webSocketMap = WebSocketMobileServer.getWebSocketMap();

+ 5 - 10
themis-exam/src/main/java/com/qmth/themis/exam/websocket/WebSocketMobileServer.java

@@ -162,8 +162,9 @@ public class WebSocketMobileServer implements Concurrently {
      * @param error
      */
     @OnError
-    public void onError(Session session, Throwable error) {
+    public void onError(Session session, Throwable error) throws IOException {
         log.error("用户错误:{},原因:{}", this.websocketSessionId, error);
+        close(this);
         throw new BusinessException(error.getMessage());
     }
 
@@ -240,16 +241,10 @@ public class WebSocketMobileServer implements Concurrently {
      *
      * @param webSocketMobileServer
      */
-    public static void close(WebSocketMobileServer webSocketMobileServer) {
+    public static void close(WebSocketMobileServer webSocketMobileServer) throws IOException {
         //判断当前连接是否还在线
-        if (Objects.nonNull(webSocketMobileServer) && webSocketMobileServer.session.isOpen()) {
-            try {
-                // 关闭连接
-                CloseReason closeReason = new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "鉴权失败!");
-                webSocketMobileServer.session.close(closeReason);
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
+        if (Objects.nonNull(webSocketMobileServer) && Objects.nonNull(webSocketMobileServer.session) && webSocketMobileServer.session.isOpen()) {
+            webSocketMobileServer.session.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, SystemConstant.WEBSOCKET_CLOSE));
         }
     }
 

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

@@ -8,17 +8,17 @@ import com.qmth.themis.business.constant.SystemConstant;
 import com.qmth.themis.business.dto.MqDto;
 import com.qmth.themis.business.dto.WebsocketDto;
 import com.qmth.themis.business.entity.TBSession;
-import com.qmth.themis.business.enums.*;
+import com.qmth.themis.business.enums.ExamRecordStatusEnum;
+import com.qmth.themis.business.enums.MqTagEnum;
+import com.qmth.themis.business.enums.WebsocketStatusEnum;
+import com.qmth.themis.business.enums.WebsocketTypeEnum;
 import com.qmth.themis.business.service.MqDtoService;
 import com.qmth.themis.business.service.TOeExamRecordService;
 import com.qmth.themis.business.util.*;
 import com.qmth.themis.common.contanst.Constants;
-import com.qmth.themis.common.enums.ExceptionResultEnum;
-import com.qmth.themis.common.enums.Platform;
 import com.qmth.themis.common.exception.BusinessException;
 import com.qmth.themis.exam.config.ExamConstant;
 import com.qmth.themis.exam.listener.service.MqOeLogicService;
-import com.qmth.themis.exam.websocket.interceptor.WebSocketMobileHandshakeInterceptor;
 import com.qmth.themis.exam.websocket.interceptor.WebSocketOeHandshakeInterceptor;
 import com.qmth.themis.exam.websocketTemplete.WebSocketOeMessageTemplete;
 import com.qmth.themis.mq.templete.Concurrently;
@@ -179,8 +179,9 @@ public class WebSocketOeServer implements Concurrently {
      * @param error
      */
     @OnError
-    public void onError(Session session, Throwable error) {
+    public void onError(Session session, Throwable error) throws IOException {
         log.error("用户错误:{},原因:{}", this.websocketSessionId, error);
+        close(this);
         throw new BusinessException(error.getMessage());
     }
 
@@ -278,16 +279,10 @@ public class WebSocketOeServer implements Concurrently {
      *
      * @param webSocketOeServer
      */
-    public static void close(WebSocketOeServer webSocketOeServer) {
+    public static void close(WebSocketOeServer webSocketOeServer) throws IOException {
         //判断当前连接是否还在线
-        if (Objects.nonNull(webSocketOeServer) && webSocketOeServer.session.isOpen()) {
-            try {
-                // 关闭连接
-                CloseReason closeReason = new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "鉴权失败!");
-                webSocketOeServer.session.close(closeReason);
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
+        if (Objects.nonNull(webSocketOeServer) && Objects.nonNull(webSocketOeServer.session) && webSocketOeServer.session.isOpen()) {
+            webSocketOeServer.session.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, SystemConstant.WEBSOCKET_CLOSE));
         }
     }