refactor: 实时在线用户统计代码重构优化
This commit is contained in:
@@ -4,12 +4,14 @@ import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.jwt.JWTPayload;
|
||||
import cn.hutool.jwt.JWTUtil;
|
||||
import com.youlai.system.common.constant.SecurityConstants;
|
||||
import com.youlai.system.event.UserConnectionEvent;
|
||||
import com.youlai.system.service.WebsocketService;
|
||||
import groovy.lang.Lazy;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.messaging.Message;
|
||||
@@ -36,8 +38,11 @@ import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerCo
|
||||
@Slf4j
|
||||
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
|
||||
|
||||
@Autowired
|
||||
private WebsocketService websocketService;
|
||||
private final ApplicationEventPublisher eventPublisher;
|
||||
|
||||
public WebSocketConfig(ApplicationEventPublisher eventPublisher) {
|
||||
this.eventPublisher = eventPublisher;
|
||||
}
|
||||
/**
|
||||
* 注册一个端点,客户端通过这个端点进行连接
|
||||
*/
|
||||
@@ -88,13 +93,13 @@ public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
|
||||
String username = JWTUtil.parseToken(bearerToken).getPayloads().getStr(JWTPayload.SUBJECT);
|
||||
if (StrUtil.isNotBlank(username)) {
|
||||
accessor.setUser(() -> username);
|
||||
websocketService.addUser(username);
|
||||
eventPublisher.publishEvent(new UserConnectionEvent(this, username, true));
|
||||
}
|
||||
}
|
||||
} else if (StompCommand.DISCONNECT.equals(accessor.getCommand())) {
|
||||
if (accessor.getUser() != null) {
|
||||
String username = accessor.getUser().getName();
|
||||
websocketService.removeUser(username);
|
||||
eventPublisher.publishEvent(new UserConnectionEvent(this, username, false));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.youlai.system.event;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
@Getter
|
||||
public class UserConnectionEvent extends ApplicationEvent {
|
||||
private final String username;
|
||||
private final boolean connected;
|
||||
|
||||
public UserConnectionEvent(Object source, String username, boolean connected) {
|
||||
super(source);
|
||||
this.username = username;
|
||||
this.connected = connected;
|
||||
}
|
||||
}
|
||||
@@ -6,5 +6,4 @@ public interface WebsocketService {
|
||||
|
||||
void removeUser(String username) ;
|
||||
|
||||
int getOnlineUserCount() ;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package com.youlai.system.service.impl;
|
||||
|
||||
import com.youlai.system.event.UserConnectionEvent;
|
||||
import com.youlai.system.service.WebsocketService;
|
||||
import groovy.lang.Lazy;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -12,13 +13,13 @@ import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class WebsocketServiceImpl implements WebsocketService {
|
||||
|
||||
@Lazy
|
||||
@Autowired
|
||||
private SimpMessagingTemplate messagingTemplate;
|
||||
private final SimpMessagingTemplate messagingTemplate;
|
||||
|
||||
private static final Set<String> onlineUsers = ConcurrentHashMap.newKeySet();
|
||||
private final Set<String> onlineUsers = ConcurrentHashMap.newKeySet();
|
||||
|
||||
@Override
|
||||
public void addUser(String username) {
|
||||
@@ -30,15 +31,22 @@ public class WebsocketServiceImpl implements WebsocketService {
|
||||
onlineUsers.remove(username);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOnlineUserCount() {
|
||||
return onlineUsers.size();
|
||||
@EventListener
|
||||
public void handleUserConnectionEvent(UserConnectionEvent event) {
|
||||
String username = event.getUsername();
|
||||
if (event.isConnected()) {
|
||||
onlineUsers.add(username);
|
||||
log.info("User connected: {}", username);
|
||||
} else {
|
||||
onlineUsers.remove(username);
|
||||
log.info("User disconnected: {}", username);
|
||||
}
|
||||
// 推送在线用户人数
|
||||
messagingTemplate.convertAndSend("/topic/onlineUserCount", onlineUsers.size());
|
||||
}
|
||||
|
||||
@Scheduled(fixedRate = 5000)
|
||||
public void sendOnlineUserCount() {
|
||||
int onlineUserCount = this.getOnlineUserCount();
|
||||
messagingTemplate.convertAndSend("/topic/onlineUserCount", onlineUserCount);
|
||||
messagingTemplate.convertAndSend("/topic/onlineUserCount", onlineUsers.size());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user