|
@@ -0,0 +1,60 @@
|
|
|
+package cn.com.qmth.examcloud.ws.handler;
|
|
|
+
|
|
|
+import cn.com.qmth.examcloud.api.commons.security.bean.User;
|
|
|
+import cn.com.qmth.examcloud.ws.api.enums.WebSocketEventType;
|
|
|
+import cn.com.qmth.examcloud.ws.handler.bean.FileAnswerBean;
|
|
|
+import cn.com.qmth.examcloud.ws.starter.core.Message;
|
|
|
+import cn.com.qmth.examcloud.ws.starter.core.MessageHandler;
|
|
|
+import com.mysql.cj.util.StringUtils;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Component
|
|
|
+@MessageHandler("fileAnswer")
|
|
|
+public class FileAnswerHandler {
|
|
|
+
|
|
|
+ @MessageHandler
|
|
|
+ public Map<String,Object> message(User user, @Message FileAnswerBean fileAnswerBean) {
|
|
|
+ try {
|
|
|
+ //事件类型
|
|
|
+ String eventType = fileAnswerBean.getEventType();
|
|
|
+
|
|
|
+ if (StringUtils.isNullOrEmpty(eventType)) {
|
|
|
+ return buildSystemErrorMap("无效的消息请求");
|
|
|
+ }
|
|
|
+ if (eventType.equals(WebSocketEventType.HEARTBEAT.toString())) {
|
|
|
+ return buildHeartbeatMessage(eventType);
|
|
|
+ } else {
|
|
|
+ return buildSystemErrorMap("无效的消息请求");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ return buildSystemErrorMap("无效的消息请求,详情:" + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建系统错误消息体
|
|
|
+ *
|
|
|
+ * @param message
|
|
|
+ */
|
|
|
+ private Map<String,Object> buildSystemErrorMap(String message) {
|
|
|
+ Map<String,Object> errorMap = new HashMap<>();
|
|
|
+ errorMap.put("eventType", WebSocketEventType.SYSTEM_ERROR.toString());
|
|
|
+ errorMap.put("errorMessage", message);
|
|
|
+ return errorMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建心跳消息体
|
|
|
+ * @param eventType
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Map<String,Object> buildHeartbeatMessage(String eventType) {
|
|
|
+ Map<String,Object> heartbeatMap = new HashMap<>();
|
|
|
+ heartbeatMap.put("eventType", eventType);
|
|
|
+ return heartbeatMap;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|