feat: 添加WebSocket服务层
This commit is contained in:
+10
@@ -0,0 +1,10 @@
|
|||||||
|
package cn.novalon.manage.sys.core.service;
|
||||||
|
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
public interface IWebSocketService {
|
||||||
|
Mono<Void> sendToUser(Long userId, Object message);
|
||||||
|
Mono<Void> broadcast(Object message);
|
||||||
|
Mono<Void> notifyNewNotice(String noticeTitle, String noticeContent);
|
||||||
|
Mono<Void> notifyNewMessage(Long userId, String title, String content);
|
||||||
|
}
|
||||||
+55
@@ -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<Void> sendToUser(Long userId, Object message) {
|
||||||
|
return Mono.fromRunnable(() -> {
|
||||||
|
webSocketHandler.sendMessageToUser(String.valueOf(userId), message);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<Void> broadcast(Object message) {
|
||||||
|
return Mono.fromRunnable(() -> {
|
||||||
|
webSocketHandler.broadcastMessage(message);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<Void> notifyNewNotice(String noticeTitle, String noticeContent) {
|
||||||
|
Map<String, Object> 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<Void> notifyNewMessage(Long userId, String title, String content) {
|
||||||
|
Map<String, Object> notification = new HashMap<>();
|
||||||
|
notification.put("type", "message");
|
||||||
|
notification.put("title", title);
|
||||||
|
notification.put("content", content);
|
||||||
|
notification.put("timestamp", System.currentTimeMillis());
|
||||||
|
|
||||||
|
return sendToUser(userId, notification);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user