update 优化 !pr317 代码结构

feature/model
疯狂的狮子li 2 years ago
parent b022c12419
commit 210570c005

@ -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());
});

Loading…
Cancel
Save