From 8401216ee86223484826dfe6af62117660795de2 Mon Sep 17 00:00:00 2001 From: liu Date: Thu, 26 Oct 2023 16:00:44 +0800 Subject: [PATCH] =?UTF-8?q?=E9=97=AE=E8=AF=8Asocket=E6=B5=81=E7=A8=8B?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/TestController.java | 13 ++++++++++++ .../com/supervision/paddlespeech/TtsUtil.java | 2 +- .../dto/res/PaddleSpeechResDTO.java | 2 +- .../service/impl/AskServiceImpl.java | 2 +- .../handler/AskWebSocketHandler.java | 20 +++++++++++++++---- 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/virtual-patient-web/src/main/java/com/supervision/controller/TestController.java b/virtual-patient-web/src/main/java/com/supervision/controller/TestController.java index dcfc118f..97539e14 100644 --- a/virtual-patient-web/src/main/java/com/supervision/controller/TestController.java +++ b/virtual-patient-web/src/main/java/com/supervision/controller/TestController.java @@ -1,5 +1,9 @@ package com.supervision.controller; +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.io.file.FileReader; +import cn.hutool.core.util.StrUtil; +import cn.hutool.json.JSONUtil; import com.supervision.model.ConfigPhysicalTool; import com.supervision.service.ConfigPhysicalToolService; import lombok.RequiredArgsConstructor; @@ -7,6 +11,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.io.File; import java.util.List; @RestController @@ -25,4 +30,12 @@ public class TestController { public List testQueryJSON(){ return configPhysicalToolService.list(); } + + + public static void main(String[] args) { + File file = FileUtil.newFile("/Users/flevance/Desktop/template.txt"); + List strings = FileReader.create(file).readLines(); + System.out.println(JSONUtil.toJsonStr(strings)); + + } } diff --git a/virtual-patient-web/src/main/java/com/supervision/paddlespeech/TtsUtil.java b/virtual-patient-web/src/main/java/com/supervision/paddlespeech/TtsUtil.java index e43c6ab8..776e7b14 100644 --- a/virtual-patient-web/src/main/java/com/supervision/paddlespeech/TtsUtil.java +++ b/virtual-patient-web/src/main/java/com/supervision/paddlespeech/TtsUtil.java @@ -29,7 +29,7 @@ public class TtsUtil { } return response.getResult().getAudio(); } catch (Exception e) { - throw new BusinessException("语音转换文字失败"); + throw new BusinessException("语音转换文字失败", e); } } diff --git a/virtual-patient-web/src/main/java/com/supervision/paddlespeech/dto/res/PaddleSpeechResDTO.java b/virtual-patient-web/src/main/java/com/supervision/paddlespeech/dto/res/PaddleSpeechResDTO.java index bca8f303..a56c4fa6 100644 --- a/virtual-patient-web/src/main/java/com/supervision/paddlespeech/dto/res/PaddleSpeechResDTO.java +++ b/virtual-patient-web/src/main/java/com/supervision/paddlespeech/dto/res/PaddleSpeechResDTO.java @@ -9,7 +9,7 @@ public class PaddleSpeechResDTO { private Integer code; - private String message; + private Object message; private T result; } diff --git a/virtual-patient-web/src/main/java/com/supervision/service/impl/AskServiceImpl.java b/virtual-patient-web/src/main/java/com/supervision/service/impl/AskServiceImpl.java index 144ab387..9b78e5b9 100644 --- a/virtual-patient-web/src/main/java/com/supervision/service/impl/AskServiceImpl.java +++ b/virtual-patient-web/src/main/java/com/supervision/service/impl/AskServiceImpl.java @@ -87,7 +87,7 @@ public class AskServiceImpl implements AskService { SocketMessageDTO res = new SocketMessageDTO(); res.setSocketId(socketMessageDTO.getSocketId()); res.setUserId(UserUtil.getUser().getId()); - res.setTextMessage("医生,我们有听懂您说的什么"); + res.setTextMessage("医生,我没有听懂您说的什么意思"); res.setType(1); session.sendMessage(new TextMessage(JSONUtil.toJsonStr(res))); log.info("兜底话术,需要检查上面是不是出什么问题了"); diff --git a/virtual-patient-web/src/main/java/com/supervision/websocket/handler/AskWebSocketHandler.java b/virtual-patient-web/src/main/java/com/supervision/websocket/handler/AskWebSocketHandler.java index f05b4729..e906db3e 100644 --- a/virtual-patient-web/src/main/java/com/supervision/websocket/handler/AskWebSocketHandler.java +++ b/virtual-patient-web/src/main/java/com/supervision/websocket/handler/AskWebSocketHandler.java @@ -3,6 +3,7 @@ package com.supervision.websocket.handler; import cn.hutool.json.JSONUtil; import com.supervision.service.AskService; import com.supervision.util.SpringBeanUtil; +import com.supervision.util.UserUtil; import com.supervision.websocket.cache.WebSocketUserCache; import com.supervision.websocket.dto.SocketMessageDTO; import lombok.extern.slf4j.Slf4j; @@ -33,10 +34,21 @@ public class AskWebSocketHandler extends TextWebSocketHandler { protected void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException { // 处理接收到的消息 log.info("收到消息:{}", message.toString()); - // 这里反序列化消息,将消息形成固定的格式 - SocketMessageDTO socketMessageDTO = JSONUtil.toBean(message.getPayload(), SocketMessageDTO.class); - AskService askService = SpringBeanUtil.getBean(AskService.class); - askService.handlerMessageBySocket(socketMessageDTO); + try { + // 这里反序列化消息,将消息形成固定的格式 + SocketMessageDTO socketMessageDTO = JSONUtil.toBean(message.getPayload(), SocketMessageDTO.class); + AskService askService = SpringBeanUtil.getBean(AskService.class); + askService.handlerMessageBySocket(socketMessageDTO); + } catch (Exception e) { + log.error("对话出现异常", e); + SocketMessageDTO res = new SocketMessageDTO(); + res.setSocketId(session.getId()); + res.setUserId(UserUtil.getUser().getId()); + res.setTextMessage("出错了"); + res.setType(1); + session.sendMessage(new TextMessage(JSONUtil.toJsonStr(res))); + } + } @Override