feat: 字典实时同步和 websocket 重构优化
This commit is contained in:
@@ -5,7 +5,7 @@ import org.springframework.web.bind.annotation.*;
|
||||
/**
|
||||
* 邮件控制层
|
||||
*
|
||||
* @author Ray
|
||||
* @author Ray.Hao
|
||||
* @since 2.10.0
|
||||
*/
|
||||
@RestController
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
package com.youlai.boot.shared.websocket.handler;
|
||||
|
||||
|
||||
import com.youlai.boot.shared.websocket.service.OnlineUserService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 在线用户定时任务
|
||||
*
|
||||
* @since 2024/10/7
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class OnlineUserJobHandler {
|
||||
|
||||
private final OnlineUserService onlineUserService;
|
||||
private final SimpMessagingTemplate messagingTemplate;
|
||||
|
||||
// 每分钟统计一次在线用户数
|
||||
@Scheduled(cron = "0 * * * * ?")
|
||||
public void execute() {
|
||||
log.info("定时任务:统计在线用户数");
|
||||
// 推送在线用户人数
|
||||
messagingTemplate.convertAndSend("/topic/onlineUserCount", onlineUserService.getOnlineUserCount());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.youlai.boot.shared.websocket.listener;
|
||||
|
||||
import com.youlai.boot.shared.websocket.service.OnlineUserService;
|
||||
import com.youlai.boot.system.event.UserConnectionEvent;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 在线用户监听器
|
||||
*
|
||||
* @author haoxr
|
||||
* @since 2024/9/25
|
||||
*/
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class OnlineUserListener {
|
||||
|
||||
private final SimpMessagingTemplate messagingTemplate;
|
||||
private final OnlineUserService onlineUserService;
|
||||
|
||||
/**
|
||||
* 用户连接事件处理
|
||||
*
|
||||
* @param event 用户连接事件
|
||||
*/
|
||||
@EventListener
|
||||
public void handleUserConnectionEvent(UserConnectionEvent event) {
|
||||
String username = event.getUsername();
|
||||
if (event.isConnected()) {
|
||||
onlineUserService.addOnlineUser(username);
|
||||
log.info("User connected: {}", username);
|
||||
} else {
|
||||
onlineUserService.removeOnlineUser(username);
|
||||
log.info("User disconnected: {}", username);
|
||||
}
|
||||
// 推送在线用户人数
|
||||
messagingTemplate.convertAndSend("/topic/onlineUserCount", onlineUserService.getOnlineUserCount());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
package com.youlai.boot.shared.websocket.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 在线用户服务
|
||||
*
|
||||
* @author haoxr
|
||||
* @since 2024/9/26
|
||||
*/
|
||||
|
||||
@Service
|
||||
public class OnlineUserService {
|
||||
|
||||
private final Set<String> onlineUsers = ConcurrentHashMap.newKeySet();
|
||||
|
||||
/**
|
||||
* 添加用户到在线用户集合
|
||||
*
|
||||
* @param username 用户名
|
||||
*/
|
||||
public void addOnlineUser(String username) {
|
||||
onlineUsers.add(username);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从在线用户集合移除用户
|
||||
*
|
||||
* @param username 用户名
|
||||
*/
|
||||
public void removeOnlineUser(String username) {
|
||||
onlineUsers.remove(username);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有在线用户
|
||||
*
|
||||
* @return 在线用户集合
|
||||
*/
|
||||
public Set<String> getAllOnlineUsers() {
|
||||
return Collections.unmodifiableSet(onlineUsers);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取在线的接收者
|
||||
* 从所有接收者中过滤出在线的接收者
|
||||
*
|
||||
* @param receivers 接收者
|
||||
* @return 在线的接收者集合
|
||||
*/
|
||||
public Set<String> getOnlineReceivers(Set<String> receivers) {
|
||||
return receivers.stream().filter(onlineUsers::contains).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取在线用户数量
|
||||
*
|
||||
* @return 在线用户数量
|
||||
*/
|
||||
public int getOnlineUserCount() {
|
||||
return onlineUsers.size();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user