mirror of
				https://github.com/dromara/RuoYi-Vue-Plus.git
				synced 2025-11-04 16:23:42 +08:00 
			
		
		
		
	update 优化 !pr317 代码结构
This commit is contained in:
		@@ -2,12 +2,10 @@ package com.ruoyi.common.websocket.config;
 | 
			
		||||
 | 
			
		||||
import cn.hutool.core.util.StrUtil;
 | 
			
		||||
import com.ruoyi.common.websocket.config.properties.WebSocketProperties;
 | 
			
		||||
import com.ruoyi.common.websocket.constant.WebSocketConstants;
 | 
			
		||||
import com.ruoyi.common.websocket.handler.PlusWebSocketHandler;
 | 
			
		||||
import com.ruoyi.common.websocket.interceptor.PlusWebSocketInterceptor;
 | 
			
		||||
import com.ruoyi.common.websocket.listener.WebSocketTopicListener;
 | 
			
		||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
 | 
			
		||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
 | 
			
		||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 | 
			
		||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
 | 
			
		||||
import org.springframework.context.annotation.Bean;
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,6 @@ package com.ruoyi.common.websocket.config.properties;
 | 
			
		||||
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import org.springframework.boot.context.properties.ConfigurationProperties;
 | 
			
		||||
import org.springframework.validation.annotation.Validated;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * WebSocket 配置项
 | 
			
		||||
@@ -13,7 +12,7 @@ import org.springframework.validation.annotation.Validated;
 | 
			
		||||
@Data
 | 
			
		||||
public class WebSocketProperties {
 | 
			
		||||
 | 
			
		||||
    private Boolean enable;
 | 
			
		||||
    private Boolean enabled;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 路径
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,7 @@ package com.ruoyi.common.websocket.dto;
 | 
			
		||||
import lombok.Builder;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
 | 
			
		||||
import java.io.Serial;
 | 
			
		||||
import java.io.Serializable;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
@@ -15,6 +16,7 @@ import java.util.List;
 | 
			
		||||
@Data
 | 
			
		||||
public class WebSocketMessageDto implements Serializable {
 | 
			
		||||
 | 
			
		||||
    @Serial
 | 
			
		||||
    private static final long serialVersionUID = 1L;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 
 | 
			
		||||
@@ -22,8 +22,6 @@ public class PlusWebSocketHandler extends AbstractWebSocketHandler {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 连接成功后
 | 
			
		||||
     *
 | 
			
		||||
     * @param session
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public void afterConnectionEstablished(WebSocketSession session) {
 | 
			
		||||
 
 | 
			
		||||
@@ -2,9 +2,7 @@ package com.ruoyi.common.websocket.utils;
 | 
			
		||||
 | 
			
		||||
import cn.hutool.core.collection.CollUtil;
 | 
			
		||||
import com.ruoyi.common.core.domain.model.LoginUser;
 | 
			
		||||
import com.ruoyi.common.json.utils.JsonUtils;
 | 
			
		||||
import com.ruoyi.common.redis.utils.RedisUtils;
 | 
			
		||||
import com.ruoyi.common.satoken.utils.LoginHelper;
 | 
			
		||||
import com.ruoyi.common.websocket.dto.WebSocketMessageDto;
 | 
			
		||||
import com.ruoyi.common.websocket.holder.WebSocketSessionHolder;
 | 
			
		||||
import lombok.AccessLevel;
 | 
			
		||||
@@ -34,8 +32,9 @@ public class WebSocketUtils {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 发送消息
 | 
			
		||||
     * @param sessionKey
 | 
			
		||||
     * @param message
 | 
			
		||||
     *
 | 
			
		||||
     * @param sessionKey session主键 一般为用户id
 | 
			
		||||
     * @param message    消息文本
 | 
			
		||||
     */
 | 
			
		||||
    public static void sendMessage(Long sessionKey, String message) {
 | 
			
		||||
        WebSocketSession session = WebSocketSessionHolder.getSessions(sessionKey);
 | 
			
		||||
@@ -45,7 +44,7 @@ public class WebSocketUtils {
 | 
			
		||||
    /**
 | 
			
		||||
     * 订阅消息
 | 
			
		||||
     *
 | 
			
		||||
     * @param consumer
 | 
			
		||||
     * @param consumer 自定义处理
 | 
			
		||||
     */
 | 
			
		||||
    public static void subscribeMessage(Consumer<WebSocketMessageDto> consumer) {
 | 
			
		||||
        RedisUtils.subscribe(WEB_SOCKET_TOPIC, WebSocketMessageDto.class, consumer);
 | 
			
		||||
@@ -54,12 +53,12 @@ public class WebSocketUtils {
 | 
			
		||||
    /**
 | 
			
		||||
     * 发布订阅的消息
 | 
			
		||||
     *
 | 
			
		||||
     * @param webSocketMessage
 | 
			
		||||
     * @param webSocketMessage 消息对象
 | 
			
		||||
     */
 | 
			
		||||
    public static void publishMessage(WebSocketMessageDto webSocketMessage) {
 | 
			
		||||
        List<Long> unsentSessionKeys = new ArrayList<>();
 | 
			
		||||
        // 当前服务内session,直接发送消息
 | 
			
		||||
        for (Long sessionKey: webSocketMessage.getSessionKeys()) {
 | 
			
		||||
        for (Long sessionKey : webSocketMessage.getSessionKeys()) {
 | 
			
		||||
            if (WebSocketSessionHolder.existSession(sessionKey)) {
 | 
			
		||||
                WebSocketUtils.sendMessage(sessionKey, webSocketMessage.getMessage());
 | 
			
		||||
                continue;
 | 
			
		||||
@@ -70,7 +69,7 @@ public class WebSocketUtils {
 | 
			
		||||
        if (CollUtil.isNotEmpty(unsentSessionKeys)) {
 | 
			
		||||
            WebSocketMessageDto broadcastMessage = WebSocketMessageDto.builder()
 | 
			
		||||
                .message(webSocketMessage.getMessage()).sessionKeys(unsentSessionKeys).build();
 | 
			
		||||
            RedisUtils.publish(WEB_SOCKET_TOPIC, broadcastMessage,  consumer -> {
 | 
			
		||||
            RedisUtils.publish(WEB_SOCKET_TOPIC, broadcastMessage, consumer -> {
 | 
			
		||||
                log.info(" WebSocket发送主题订阅消息topic:{} session keys:{} message:{}",
 | 
			
		||||
                    WEB_SOCKET_TOPIC, unsentSessionKeys, webSocketMessage.getMessage());
 | 
			
		||||
            });
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user