refactor: migrate WebSocket handler to manage-notify module

This commit is contained in:
张翔
2026-03-14 10:31:12 +08:00
parent 5840a880e3
commit 4db2019d95
4 changed files with 7 additions and 86 deletions
+4
View File
@@ -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>
@@ -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,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) -> {
@@ -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);
}
}