From 21c0a94a5d9d1635d9dce2749ef6eaad8ce65439 Mon Sep 17 00:00:00 2001 From: KilLze Date: Wed, 7 Jan 2026 03:43:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BC=9A=E8=AF=9D=EF=BC=8C?= =?UTF-8?q?=E5=BD=93=E5=AF=B9=E5=88=A0=E9=99=A4=E7=9A=84=E4=BC=9A=E8=AF=9D?= =?UTF-8?q?=E5=8F=91=E9=80=81=E4=BF=A1=E6=81=AF=E6=97=B6=E8=BF=94=E5=9B=9E?= =?UTF-8?q?error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/websocket/ChatWebSocketHandler.java | 12 +++++++++--- .../com/bao/dating/service/impl/ChatServiceImpl.java | 3 ++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/bao/dating/controller/websocket/ChatWebSocketHandler.java b/src/main/java/com/bao/dating/controller/websocket/ChatWebSocketHandler.java index a0ad6e2..3624643 100644 --- a/src/main/java/com/bao/dating/controller/websocket/ChatWebSocketHandler.java +++ b/src/main/java/com/bao/dating/controller/websocket/ChatWebSocketHandler.java @@ -64,18 +64,24 @@ public class ChatWebSocketHandler extends TextWebSocketHandler { // 处理私聊消息 if ("chat".equals(wsMessage.getType())) { - handlePrivateChat(senderUserId, wsMessage.getData()); + handlePrivateChat(session, senderUserId, wsMessage.getData()); } } /** * 私聊处理 */ - private void handlePrivateChat(Long senderUserId, ChatRecordSendDTO dto) throws Exception { + private void handlePrivateChat(WebSocketSession session, Long senderUserId, ChatRecordSendDTO dto) throws Exception { // 1. 消息入库 + 会话更新 ChatRecordsVO chatRecordsVO = chatService.createSession(senderUserId, dto); - + if (chatRecordsVO == null){ + WsMessage errorMsg = new WsMessage<>(); + errorMsg.setType("error"); + errorMsg.setData("会话已删除,无法发送消息"); + session.sendMessage(new TextMessage(objectMapper.writeValueAsString(errorMsg))); + return; + } // 2. 推送给接收方 WebSocketSession receiverSession = sessionManager.getSession(dto.getReceiverUserId()); diff --git a/src/main/java/com/bao/dating/service/impl/ChatServiceImpl.java b/src/main/java/com/bao/dating/service/impl/ChatServiceImpl.java index 2347d18..85be330 100644 --- a/src/main/java/com/bao/dating/service/impl/ChatServiceImpl.java +++ b/src/main/java/com/bao/dating/service/impl/ChatServiceImpl.java @@ -52,7 +52,8 @@ public class ChatServiceImpl implements ChatService { ChatSessions session = chatSessionsMapper.getSession(senderUserId, dto.getReceiverUserId()); if (session != null && session.getSessionStatus() == 3) { - throw new RuntimeException("会话已删除,无法发送消息"); + log.warn("会话已删除,无法发送消息"); + return null; } ChatRecords record = new ChatRecords();