refactor: migrate WebSocket handler to manage-notify module
This commit is contained in:
@@ -26,6 +26,10 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
|
||||
+2
-8
@@ -1,6 +1,6 @@
|
||||
package cn.novalon.manage.sys.config;
|
||||
package cn.novalon.manage.notify.config;
|
||||
|
||||
import cn.novalon.manage.sys.websocket.SysWebSocketHandler;
|
||||
import cn.novalon.manage.notify.websocket.SysWebSocketHandler;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.Ordered;
|
||||
@@ -12,12 +12,6 @@ import org.springframework.web.reactive.socket.server.support.WebSocketHandlerAd
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* WebSocket配置类
|
||||
*
|
||||
* @author 张翔
|
||||
* @date 2026-03-13
|
||||
*/
|
||||
@Configuration
|
||||
public class WebSocketConfig {
|
||||
|
||||
+1
-17
@@ -1,4 +1,4 @@
|
||||
package cn.novalon.manage.sys.websocket;
|
||||
package cn.novalon.manage.notify.websocket;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
@@ -14,16 +14,6 @@ import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* WebSocket处理器
|
||||
*
|
||||
* 文件定义:处理WebSocket连接和消息推送
|
||||
* 涉及业务:实时消息推送、系统公告通知、用户消息
|
||||
* 算法:使用ConcurrentHashMap管理WebSocket会话,支持点对点和广播消息,添加心跳机制和超时处理
|
||||
*
|
||||
* @author 张翔
|
||||
* @date 2026-03-13
|
||||
*/
|
||||
@Component
|
||||
public class SysWebSocketHandler implements WebSocketHandler {
|
||||
|
||||
@@ -61,9 +51,6 @@ public class SysWebSocketHandler implements WebSocketHandler {
|
||||
.then();
|
||||
}
|
||||
|
||||
/**
|
||||
* 定时清理空闲连接
|
||||
*/
|
||||
@Scheduled(fixedRate = 60000)
|
||||
public void cleanupIdleConnections() {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
@@ -86,9 +73,6 @@ public class SysWebSocketHandler implements WebSocketHandler {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 定时发送心跳消息
|
||||
*/
|
||||
@Scheduled(fixedRate = 30000)
|
||||
public void sendHeartbeat() {
|
||||
sessions.forEach((userId, session) -> {
|
||||
-61
@@ -1,61 +0,0 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* WebSocket服务实现类
|
||||
*
|
||||
* @author 张翔
|
||||
* @date 2026-03-14
|
||||
*/
|
||||
@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