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); + } +}