From 0cebc8534db7775adb4f3a2fb4a9dd54cc28b6ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=BF=94?= Date: Thu, 12 Mar 2026 07:50:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0WebSocket=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sys/core/service/IWebSocketService.java | 10 ++++ .../service/impl/WebSocketServiceImpl.java | 55 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 novalon-manage-api/manage-sys/src/main/java/cn/novalon/manage/sys/core/service/IWebSocketService.java create mode 100644 novalon-manage-api/manage-sys/src/main/java/cn/novalon/manage/sys/core/service/impl/WebSocketServiceImpl.java diff --git a/novalon-manage-api/manage-sys/src/main/java/cn/novalon/manage/sys/core/service/IWebSocketService.java b/novalon-manage-api/manage-sys/src/main/java/cn/novalon/manage/sys/core/service/IWebSocketService.java new file mode 100644 index 0000000..425e7bb --- /dev/null +++ b/novalon-manage-api/manage-sys/src/main/java/cn/novalon/manage/sys/core/service/IWebSocketService.java @@ -0,0 +1,10 @@ +package cn.novalon.manage.sys.core.service; + +import reactor.core.publisher.Mono; + +public interface IWebSocketService { + Mono sendToUser(Long userId, Object message); + Mono broadcast(Object message); + Mono notifyNewNotice(String noticeTitle, String noticeContent); + Mono notifyNewMessage(Long userId, String title, String content); +} diff --git a/novalon-manage-api/manage-sys/src/main/java/cn/novalon/manage/sys/core/service/impl/WebSocketServiceImpl.java b/novalon-manage-api/manage-sys/src/main/java/cn/novalon/manage/sys/core/service/impl/WebSocketServiceImpl.java new file mode 100644 index 0000000..c3d7eb2 --- /dev/null +++ b/novalon-manage-api/manage-sys/src/main/java/cn/novalon/manage/sys/core/service/impl/WebSocketServiceImpl.java @@ -0,0 +1,55 @@ +package cn.novalon.manage.sys.core.service.impl; + +import cn.novalon.manage.sys.core.service.IWebSocketService; +import cn.novalon.manage.sys.websocket.SysWebSocketHandler; +import org.springframework.stereotype.Service; +import reactor.core.publisher.Mono; + +import java.util.HashMap; +import java.util.Map; + +@Service +public class WebSocketServiceImpl implements IWebSocketService { + + private final SysWebSocketHandler webSocketHandler; + + public WebSocketServiceImpl(SysWebSocketHandler webSocketHandler) { + this.webSocketHandler = webSocketHandler; + } + + @Override + public Mono sendToUser(Long userId, Object message) { + return Mono.fromRunnable(() -> { + webSocketHandler.sendMessageToUser(String.valueOf(userId), message); + }); + } + + @Override + public Mono broadcast(Object message) { + return Mono.fromRunnable(() -> { + webSocketHandler.broadcastMessage(message); + }); + } + + @Override + public Mono notifyNewNotice(String noticeTitle, String noticeContent) { + Map notification = new HashMap<>(); + notification.put("type", "notice"); + notification.put("title", noticeTitle); + notification.put("content", noticeContent); + notification.put("timestamp", System.currentTimeMillis()); + + return broadcast(notification); + } + + @Override + public Mono notifyNewMessage(Long userId, String title, String content) { + Map notification = new HashMap<>(); + notification.put("type", "message"); + notification.put("title", title); + notification.put("content", content); + notification.put("timestamp", System.currentTimeMillis()); + + return sendToUser(userId, notification); + } +}