فهرست منبع

feat:智能陪练一期

Joburgess 3 سال پیش
والد
کامیت
64afd52f8b

+ 67 - 10
mec-biz/src/main/java/com/ym/mec/biz/dal/dto/WebSocketInfo.java

@@ -1,32 +1,89 @@
 package com.ym.mec.biz.dal.dto;
 
-import com.alibaba.fastjson.JSONObject;
-
-import java.util.HashMap;
+import com.ym.mec.biz.dal.enums.WebsocketTypeEnum;
+import org.springframework.http.HttpStatus;
 
 /**
  * @Author Joburgess
  * @Date 2021/6/17 0017
  */
-public class WebSocketInfo {
+public class WebSocketInfo<T> {
+
+    private Head header = new Head();
+
+    private T body;
+
+    public static class Head{
+        private HttpStatus status = HttpStatus.OK;
+        private String commond = "";
+        private WebsocketTypeEnum type;
+
+        public Head() {
+        }
+
+        public Head(HttpStatus status) {
+            this.status = status;
+        }
+
+        public Head(String commond) {
+            this.commond = commond;
+        }
+
+        public Head(String commond, WebsocketTypeEnum type) {
+            this.commond = commond;
+            this.type = type;
+        }
+
+        public HttpStatus getStatus() {
+            return status;
+        }
+
+        public void setStatus(HttpStatus status) {
+            this.status = status;
+        }
+
+        public String getCommond() {
+            return commond;
+        }
 
-    private HashMap<String, String> header = new HashMap<>();
+        public void setCommond(String commond) {
+            this.commond = commond;
+        }
+
+        public WebsocketTypeEnum getType() {
+            return type;
+        }
+
+        public void setType(WebsocketTypeEnum type) {
+            this.type = type;
+        }
+    }
+
+    public WebSocketInfo() {
+    }
+
+    public WebSocketInfo(Head header) {
+        this.header = header;
+    }
+
+    public static WebSocketInfo success(){
+        return new WebSocketInfo(new Head());
+    }
 
-    private Object body;
 
-    public HashMap<String, String> getHeader() {
+    public Head getHeader() {
         return header;
     }
 
-    public void setHeader(HashMap<String, String> header) {
+    public void setHeader(Head header) {
         this.header = header;
     }
 
-    public Object getBody() {
+    public T getBody() {
         return body;
     }
 
-    public void setBody(Object body) {
+    public void setBody(T body) {
         this.body = body;
     }
 }

+ 31 - 6
mec-biz/src/main/java/com/ym/mec/biz/handler/WebSocketHandler.java

@@ -1,20 +1,24 @@
 package com.ym.mec.biz.handler;
 
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
 import com.ym.mec.biz.dal.dto.WebSocketClientDetail;
 import com.ym.mec.biz.dal.dto.WebSocketInfo;
 import com.ym.mec.biz.dal.enums.WebsocketTypeEnum;
 import com.ym.mec.biz.service.SoundSocketService;
 import com.ym.mec.biz.service.WebSocketEventHandler;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.poi.ss.formula.functions.T;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
 import org.springframework.web.socket.*;
 import org.springframework.web.socket.handler.AbstractWebSocketHandler;
 
+import java.io.IOException;
 import java.util.Date;
 import java.util.Map;
+import java.util.Objects;
 import java.util.concurrent.ConcurrentHashMap;
 
 /**
@@ -24,7 +28,7 @@ import java.util.concurrent.ConcurrentHashMap;
 @Service
 public class WebSocketHandler extends AbstractWebSocketHandler {
 
-    private final Logger LOGGER = LoggerFactory.getLogger(WebSocketHandler.class);
+    private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketHandler.class);
 
     /**
      * @describe 存储客户端链接
@@ -67,13 +71,13 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
     protected void handleTextMessage(WebSocketSession session, TextMessage message){
         String phone = session.getPrincipal().getName().split(":")[1];
         LOGGER.info("{}: {}", phone, message.getPayload());
-        WebSocketInfo webSocketInfo = JSON.parseObject(message.getPayload(), WebSocketInfo.class);
+        WebSocketInfo<JSONObject> webSocketInfo = JSON.parseObject(message.getPayload(), WebSocketInfo.class);
 
-        WebsocketTypeEnum websocketType = WebsocketTypeEnum.SOUND_COMPARE;
-        if(webSocketInfo.getHeader().containsKey(SoundSocketService.TAG)){
-            websocketType = WebsocketTypeEnum.valueOf(webSocketInfo.getHeader().get(SoundSocketService.TAG));
+        if(Objects.isNull(webSocketInfo.getHeader().getType())){
+            webSocketInfo.getHeader().setType(WebsocketTypeEnum.SOUND_COMPARE);
         }
-        appMap.get(websocketType).receiveTextMessage(session, phone, message);
+
+        appMap.get(webSocketInfo.getHeader().getType()).receiveTextMessage(session, phone, webSocketInfo);
     }
 
     @Override
@@ -114,4 +118,25 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
     public boolean supportsPartialMessages() {
         return super.supportsPartialMessages();
     }
+
+    /**
+     * @describe 发送文本消息
+     * @author Joburgess
+     * @date 2021/8/23 0023
+     * @param phone: 接收人手机号
+     * @param message:
+     * @return boolean
+     */
+    public static <T> boolean sendTextMessage(String phone, T message){
+        if(!WS_CLIENTS.containsKey(phone)||!WS_CLIENTS.get(phone).getSession().isOpen()){
+            return false;
+        }
+        try {
+            WS_CLIENTS.get(phone).getSession().sendMessage(new TextMessage(JSON.toJSONString(message)));
+        } catch (IOException e) {
+            LOGGER.error("消息发送失败:{}", e);
+            return false;
+        }
+        return true;
+    }
 }

+ 2 - 3
mec-biz/src/main/java/com/ym/mec/biz/service/WebSocketEventHandler.java

@@ -1,5 +1,6 @@
 package com.ym.mec.biz.service;
 
+import com.ym.mec.biz.dal.dto.WebSocketInfo;
 import org.springframework.web.socket.BinaryMessage;
 import org.springframework.web.socket.TextMessage;
 import org.springframework.web.socket.WebSocketSession;
@@ -10,8 +11,6 @@ import org.springframework.web.socket.WebSocketSession;
  **/
 public interface WebSocketEventHandler {
 
-
-
     /**
      * @describe 连接成功
      * @author Joburgess
@@ -29,7 +28,7 @@ public interface WebSocketEventHandler {
      * @param message: 文本信息
      * @return void
      */
-    void receiveTextMessage(WebSocketSession session, String phone, TextMessage message);
+    void receiveTextMessage(WebSocketSession session, String phone, WebSocketInfo message);
 
     /**
      * @describe 接收到二进制数据

+ 2 - 1
mec-biz/src/main/java/com/ym/mec/biz/service/impl/SoundCheckHandler.java

@@ -50,7 +50,7 @@ public class SoundCheckHandler implements WebSocketEventHandler {
     }
 
     @Override
-    public void receiveTextMessage(WebSocketSession session, String phone, TextMessage message) {
+    public void receiveTextMessage(WebSocketSession session, String phone, WebSocketInfo message) {
         userSoundCheckInfo.put(phone, 0);
     }
 
@@ -68,6 +68,7 @@ public class SoundCheckHandler implements WebSocketEventHandler {
         } catch (UnsupportedAudioFileException e) {
             throw new BizException("{}校音异常:{}", phone, e);
         }
+        WebSocketHandler.sendTextMessage(phone, WebSocketInfo.success());
     }
 
     @Override

+ 5 - 18
mec-biz/src/main/java/com/ym/mec/biz/service/impl/SoundCompareHandler.java

@@ -23,7 +23,6 @@ import com.ym.mec.common.exception.BizException;
 import com.ym.mec.thirdparty.storage.StoragePluginContext;
 import com.ym.mec.thirdparty.storage.provider.KS3StoragePlugin;
 import com.ym.mec.util.upload.UploadUtil;
-import org.apache.commons.io.FileUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -31,7 +30,6 @@ import org.springframework.security.oauth2.provider.OAuth2Authentication;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 import org.springframework.web.socket.BinaryMessage;
-import org.springframework.web.socket.TextMessage;
 import org.springframework.web.socket.WebSocketSession;
 
 import javax.sound.sampled.UnsupportedAudioFileException;
@@ -88,15 +86,10 @@ public class SoundCompareHandler implements WebSocketEventHandler {
     }
 
     @Override
-    public void receiveTextMessage(WebSocketSession session, String phone, TextMessage message) {
-        WebSocketInfo webSocketInfo = JSON.parseObject(message.getPayload(), WebSocketInfo.class);
-        JSONObject bodyObject = (JSONObject) webSocketInfo.getBody();
+    public void receiveTextMessage(WebSocketSession session, String phone, WebSocketInfo message) {
+        JSONObject bodyObject = (JSONObject) message.getBody();
 
-        String commond = "";
-        if(webSocketInfo.getHeader().containsKey(SoundSocketService.COMMOND)){
-            commond = webSocketInfo.getHeader().get(SoundSocketService.COMMOND);
-        }
-        switch (commond){
+        switch (message.getHeader().getCommond()){
             case SoundSocketService.MUSIC_XML:
                 userSoundInfoMap.put(phone, new SoundCompareHelper());
                 userSoundInfoMap.get(phone).setClientId(((OAuth2Authentication)session.getPrincipal()).getOAuth2Request().getClientId());
@@ -508,9 +501,7 @@ public class SoundCompareHandler implements WebSocketEventHandler {
     private WebSocketInfo createPushInfo(String phone, String command, Integer measureIndex,
                                          BigDecimal intonation, BigDecimal cadence, BigDecimal integrity){
         WebSocketInfo webSocketInfo = new WebSocketInfo();
-        HashMap<String, String> header = new HashMap<>();
-        header.put("commond", command);
-        webSocketInfo.setHeader(header);
+        webSocketInfo.setHeader(new WebSocketInfo.Head(command));
         Map<String, Object> result = new HashMap<>(5);
         BigDecimal score = cadence;
         if(Objects.isNull(userSoundInfoMap.get(phone).getSubjectId())||userSoundInfoMap.get(phone).getSubjectId()!=23){
@@ -528,11 +519,7 @@ public class SoundCompareHandler implements WebSocketEventHandler {
         LOGGER.info("小节频分:{}", JSON.toJSONString(webSocketInfo));
 
         //推送结果
-        try {
-            WebSocketHandler.WS_CLIENTS.get(phone).getSession().sendMessage(new TextMessage(JSON.toJSONString(webSocketInfo)));
-        } catch (IOException e) {
-            LOGGER.error("{}评分结果推送失败", phone);
-        }
+        WebSocketHandler.sendTextMessage(phone, webSocketInfo);
         return webSocketInfo;
     }
 }