Merge branch '20260204'

# Conflicts:
#	.idea/compiler.xml
#	.idea/vcs.xml
#	lidee-admin/src/main/resources/application-local.yaml
#	lidee-admin/target/classes/application-local.yaml
#	lidee-admin/target/lidee-admin.jar
#	lidee-admin/target/lidee-admin.jar.original
#	lidee-admin/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst
#	lidee-core/src/main/java/com/lideeyunji/core/framework/entity/ReportFieldEntity.java
#	lidee-core/src/main/java/com/lideeyunji/core/framework/service/impl/ReportServiceImpl.java
#	lidee-core/target/classes/com/lideeyunji/core/framework/entity/ReportFieldEntity.class
#	lidee-core/target/classes/com/lideeyunji/core/framework/service/impl/ReportServiceImpl.class
#	lidee-core/target/lidee-core-2.2.4.jar
#	lidee-core/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst
#	lidee-module/lidee-module-api/target/lidee-module-api-2.2.4.jar
#	lidee-module/lidee-module-api/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst
#	lidee-module/lidee-module-biz/target/lidee-module-biz-2.2.4.jar
#	lidee-module/lidee-module-biz/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst
#	lidee-service/lidee-service-infra-api/target/lidee-service-infra-api-2.2.4.jar
#	lidee-service/lidee-service-infra-biz/target/lidee-service-infra-biz-2.2.4.jar
#	lidee-service/lidee-service-system-api/target/lidee-service-system-api-2.2.4.jar
#	lidee-service/lidee-service-system-biz/target/lidee-service-system-biz-2.2.4.jar
#	lidee-tool/tool-common/target/tool-common-2.2.4.jar
#	lidee-tool/tool-spring-boot-starter-ai/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst
#	lidee-tool/tool-spring-boot-starter-ai/target/tool-spring-boot-starter-ai-2.2.4.jar
#	lidee-tool/tool-spring-boot-starter-captcha/target/tool-spring-boot-starter-captcha-2.2.4.jar
#	lidee-tool/tool-spring-boot-starter-dict/target/tool-spring-boot-starter-dict-2.2.4.jar
#	lidee-tool/tool-spring-boot-starter-excel/target/tool-spring-boot-starter-excel-2.2.4.jar
#	lidee-tool/tool-spring-boot-starter-exception/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst
#	lidee-tool/tool-spring-boot-starter-exception/target/tool-spring-boot-starter-exception-2.2.4.jar
#	lidee-tool/tool-spring-boot-starter-file/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst
#	lidee-tool/tool-spring-boot-starter-file/target/tool-spring-boot-starter-file-2.2.4.jar
#	lidee-tool/tool-spring-boot-starter-flowable/target/tool-spring-boot-starter-flowable-2.2.4.jar
#	lidee-tool/tool-spring-boot-starter-ip/target/tool-spring-boot-starter-ip-2.2.4.jar
#	lidee-tool/tool-spring-boot-starter-job/target/tool-spring-boot-starter-job-2.2.4.jar
#	lidee-tool/tool-spring-boot-starter-monitor/target/tool-spring-boot-starter-monitor-2.2.4.jar
#	lidee-tool/tool-spring-boot-starter-mybatis/target/tool-spring-boot-starter-mybatis-2.2.4.jar
#	lidee-tool/tool-spring-boot-starter-operatelog/target/tool-spring-boot-starter-operatelog-2.2.4.jar
#	lidee-tool/tool-spring-boot-starter-permission/target/tool-spring-boot-starter-permission-2.2.4.jar
#	lidee-tool/tool-spring-boot-starter-protection/target/tool-spring-boot-starter-protection-2.2.4.jar
#	lidee-tool/tool-spring-boot-starter-redis/target/tool-spring-boot-starter-redis-2.2.4.jar
#	lidee-tool/tool-spring-boot-starter-security/target/tool-spring-boot-starter-security-2.2.4.jar
#	lidee-tool/tool-spring-boot-starter-sql/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst
#	lidee-tool/tool-spring-boot-starter-sql/target/tool-spring-boot-starter-sql-2.2.4.jar
#	lidee-tool/tool-spring-boot-starter-tenant/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst
#	lidee-tool/tool-spring-boot-starter-tenant/target/tool-spring-boot-starter-tenant-2.2.4.jar
#	lidee-tool/tool-spring-boot-starter-test/target/tool-spring-boot-starter-test-2.2.4.jar
#	lidee-tool/tool-spring-boot-starter-web/target/tool-spring-boot-starter-web-2.2.4.jar
#	lidee-tool/tool-spring-boot-starter-websocket/target/tool-spring-boot-starter-websocket-2.2.4.jar
#	lidee-tool/tool-spring-boot-starter-yunji/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst
#	lidee-tool/tool-spring-boot-starter-yunji/target/tool-spring-boot-starter-yunji-2.2.4.jar
#	logs/lideeyunji-error.log
#	logs/lideeyunji-info.log
This commit is contained in:
chy
2026-02-09 23:30:06 +08:00
1718 changed files with 66 additions and 116884 deletions

View File

@@ -1,35 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.lideeyunji</groupId>
<artifactId>lidee-tool</artifactId>
<version>2.2.4</version>
</parent>
<groupId>com.lideeyunji</groupId>
<artifactId>tool-spring-boot-starter-websocket</artifactId>
<version>2.2.4</version>
<name>${project.artifactId}</name>
<description>WebSocket 框架,支持多节点的广播</description>
<dependencies>
<dependency>
<groupId>com.lideeyunji</groupId>
<artifactId>tool-common</artifactId>
</dependency>
<dependency>
<groupId>com.lideeyunji</groupId>
<artifactId>tool-spring-boot-starter-security</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>com.lideeyunji</groupId>
<artifactId>tool-spring-boot-starter-tenant</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@@ -1,53 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.lideeyunji</groupId>
<artifactId>lidee-tool</artifactId>
<version>${lidee.version}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>tool-spring-boot-starter-websocket</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>WebSocket 框架,支持多节点的广播</description>
<dependencies>
<dependency>
<groupId>com.lideeyunji</groupId>
<artifactId>tool-common</artifactId>
</dependency>
<!-- Web 相关 -->
<dependency>
<!-- 为什么是 websocket 依赖 security 呢?而不是 security 拓展 websocket 呢?
因为 websocket 和 LoginUser 当前登录的用户有一定的相关性,具体可见 WebSocketSessionManagerImpl 逻辑。
如果让 security 拓展 websocket 的话,会导致 websocket 组件的封装很散,进而增大理解成本。
-->
<groupId>com.lideeyunji</groupId>
<artifactId>tool-spring-boot-starter-security</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<!-- 业务组件 -->
<dependency>
<!-- 为什么要依赖 tenant 组件?
因为广播某个类型的用户时候,需要根据租户过滤下,避免广播到别的租户!
-->
<groupId>com.lideeyunji</groupId>
<artifactId>tool-spring-boot-starter-tenant</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@@ -1,77 +0,0 @@
package com.lideeyunji.tool.framework.websocket.config;
import com.lideeyunji.tool.framework.websocket.core.handler.JsonWebSocketMessageHandler;
import com.lideeyunji.tool.framework.websocket.core.listener.WebSocketMessageListener;
import com.lideeyunji.tool.framework.websocket.core.security.LoginUserHandshakeInterceptor;
import com.lideeyunji.tool.framework.websocket.core.sender.local.LocalWebSocketMessageSender;
import com.lideeyunji.tool.framework.websocket.core.session.WebSocketSessionHandlerDecorator;
import com.lideeyunji.tool.framework.websocket.core.session.WebSocketSessionManager;
import com.lideeyunji.tool.framework.websocket.core.session.WebSocketSessionManagerImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.server.HandshakeInterceptor;
import java.util.List;
/**
* WebSocket 自动配置
*
* @author xingyu4j
*/
@EnableWebSocket // 开启 websocket
@ConditionalOnProperty(prefix = "lideeyunji.websocket", value = "enable", matchIfMissing = true) // 允许使用 lideeyunji.websocket.enable=false 禁用 websocket
@EnableConfigurationProperties(WebSocketProperties.class)
public class WebSocketAutoConfiguration {
@Bean
public WebSocketConfigurer webSocketConfigurer(HandshakeInterceptor[] handshakeInterceptors,
WebSocketHandler webSocketHandler,
WebSocketProperties webSocketProperties) {
return registry -> registry
// 添加 WebSocketHandler
.addHandler(webSocketHandler, webSocketProperties.getPath())
.addInterceptors(handshakeInterceptors)
// 允许跨域,否则前端连接会直接断开
.setAllowedOriginPatterns("*");
}
@Bean
public HandshakeInterceptor handshakeInterceptor() {
return new LoginUserHandshakeInterceptor();
}
@Bean
public WebSocketHandler webSocketHandler(WebSocketSessionManager sessionManager,
List<? extends WebSocketMessageListener<?>> messageListeners) {
// 1. 创建 JsonWebSocketMessageHandler 对象,处理消息
JsonWebSocketMessageHandler messageHandler = new JsonWebSocketMessageHandler(messageListeners);
// 2. 创建 WebSocketSessionHandlerDecorator 对象,处理连接
return new WebSocketSessionHandlerDecorator(messageHandler, sessionManager);
}
@Bean
public WebSocketSessionManager webSocketSessionManager() {
return new WebSocketSessionManagerImpl();
}
// ==================== Sender 相关 ====================
@Configuration
@ConditionalOnProperty(prefix = "lideeyunji.websocket", name = "sender-type", havingValue = "local", matchIfMissing = true)
public class LocalWebSocketMessageSenderConfiguration {
@Bean
public LocalWebSocketMessageSender localWebSocketMessageSender(WebSocketSessionManager sessionManager) {
return new LocalWebSocketMessageSender(sessionManager);
}
}
}

View File

@@ -1,51 +0,0 @@
package com.lideeyunji.tool.framework.websocket.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
/**
* WebSocket 配置项
*
* @author xingyu4j
*/
@ConfigurationProperties("lideeyunji.websocket")
@Data
@Validated
public class WebSocketProperties {
/**
* WebSocket 的连接路径
*/
@NotEmpty(message = "WebSocket 的连接路径不能为空")
private String path = "/ws";
/**
* 消息发送器的类型
*
* 可选值local、redis、rocketmq、kafka、rabbitmq
*/
@NotNull(message = "WebSocket 的消息发送者不能为空")
private String senderType = "local";
/** yml 所有配置
* websocket:
* enable: true # websocket的开关
* path: /infra/ws # 路径
* sender-type: local # 消息发送的类型,可选值为 local、redis、rocketmq、kafka、rabbitmq
* sender-rocketmq:
* topic: ${spring.application.name}-websocket # 消息发送的 RocketMQ Topic
* consumer-group: ${spring.application.name}-websocket-consumer # 消息发送的 RocketMQ Consumer Group
* sender-rabbitmq:
* exchange: ${spring.application.name}-websocket-exchange # 消息发送的 RabbitMQ Exchange
* queue: ${spring.application.name}-websocket-queue # 消息发送的 RabbitMQ Queue
* sender-kafka:
* topic: ${spring.application.name}-websocket # 消息发送的 Kafka Topic
* consumer-group: ${spring.application.name}-websocket-consumer # 消息发送的 Kafka Consumer Group
*
*/
}

View File

@@ -1,83 +0,0 @@
package com.lideeyunji.tool.framework.websocket.core.handler;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.TypeUtil;
import com.lideeyunji.tool.framework.common.util.json.JsonUtils;
import com.lideeyunji.tool.framework.tenant.core.util.TenantUtils;
import com.lideeyunji.tool.framework.websocket.core.listener.WebSocketMessageListener;
import com.lideeyunji.tool.framework.websocket.core.message.JsonWebSocketMessage;
import com.lideeyunji.tool.framework.websocket.core.util.WebSocketFrameworkUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
/**
* JSON 格式 {@link WebSocketHandler} 实现类
*
* 基于 {@link JsonWebSocketMessage#getType()} 消息类型,调度到对应的 {@link WebSocketMessageListener} 监听器。
*
* @author 金灯剑客
*/
@Slf4j
public class JsonWebSocketMessageHandler extends TextWebSocketHandler {
/**
* type 与 WebSocketMessageListener 的映射
*/
private final Map<String, WebSocketMessageListener<Object>> listeners = new HashMap<>();
@SuppressWarnings({"rawtypes", "unchecked"})
public JsonWebSocketMessageHandler(List<? extends WebSocketMessageListener> listenersList) {
listenersList.forEach((Consumer<WebSocketMessageListener>)
listener -> listeners.put(listener.getType(), listener));
}
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
// 1.1 空消息,跳过
if (message.getPayloadLength() == 0) {
return;
}
// 1.2 ping 心跳消息,直接返回 pong 消息。
if (message.getPayloadLength() == 4 && Objects.equals(message.getPayload(), "ping")) {
session.sendMessage(new TextMessage("pong"));
return;
}
// 2.1 解析消息
try {
JsonWebSocketMessage jsonMessage = JsonUtils.parseObject(message.getPayload(), JsonWebSocketMessage.class);
if (jsonMessage == null) {
log.error("[handleTextMessage][session({}) message({}) 解析为空]", session.getId(), message.getPayload());
return;
}
if (StrUtil.isEmpty(jsonMessage.getType())) {
log.error("[handleTextMessage][session({}) message({}) 类型为空]", session.getId(), message.getPayload());
return;
}
// 2.2 获得对应的 WebSocketMessageListener
WebSocketMessageListener<Object> messageListener = listeners.get(jsonMessage.getType());
if (messageListener == null) {
log.error("[handleTextMessage][session({}) message({}) 监听器为空]", session.getId(), message.getPayload());
return;
}
// 2.3 处理消息
Type type = TypeUtil.getTypeArgument(messageListener.getClass(), 0);
Object messageObj = JsonUtils.parseObject(jsonMessage.getContent(), type);
Long tenantId = WebSocketFrameworkUtils.getTenantId(session);
TenantUtils.execute(tenantId, () -> messageListener.onMessage(session, messageObj));
} catch (Throwable ex) {
log.error("[handleTextMessage][session({}) message({}) 处理异常]", session.getId(), message.getPayload());
}
}
}

View File

@@ -1,31 +0,0 @@
package com.lideeyunji.tool.framework.websocket.core.listener;
import com.lideeyunji.tool.framework.websocket.core.message.JsonWebSocketMessage;
import org.springframework.web.socket.WebSocketSession;
/**
* WebSocket 消息监听器接口
*
* 目的:前端发送消息给后端后,处理对应 {@link #getType()} 类型的消息
*
* @param <T> 泛型,消息类型
*/
public interface WebSocketMessageListener<T> {
/**
* 处理消息
*
* @param session Session
* @param message 消息
*/
void onMessage(WebSocketSession session, T message);
/**
* 获得消息类型
*
* @see JsonWebSocketMessage#getType()
* @return 消息类型
*/
String getType();
}

View File

@@ -1,29 +0,0 @@
package com.lideeyunji.tool.framework.websocket.core.message;
import com.lideeyunji.tool.framework.websocket.core.listener.WebSocketMessageListener;
import lombok.Data;
import java.io.Serializable;
/**
* JSON 格式的 WebSocket 消息帧
*
* @author 金灯剑客
*/
@Data
public class JsonWebSocketMessage implements Serializable {
/**
* 消息类型
*
* 目的:用于分发到对应的 {@link WebSocketMessageListener} 实现类
*/
private String type;
/**
* 消息内容
*
* 要求 JSON 对象
*/
private String content;
}

View File

@@ -1,42 +0,0 @@
package com.lideeyunji.tool.framework.websocket.core.security;
import com.lideeyunji.tool.framework.security.core.LoginUser;
import com.lideeyunji.tool.framework.security.core.filter.TokenAuthenticationFilter;
import com.lideeyunji.tool.framework.security.core.util.SecurityFrameworkUtils;
import com.lideeyunji.tool.framework.websocket.core.util.WebSocketFrameworkUtils;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.server.HandshakeInterceptor;
import java.util.Map;
/**
* 登录用户的 {@link HandshakeInterceptor} 实现类
*
* 流程如下:
* 1. 前端连接 websocket 时,会通过拼接 ?token={token} 到 ws:// 连接后,这样它可以被 {@link TokenAuthenticationFilter} 所认证通过
* 2. {@link LoginUserHandshakeInterceptor} 负责把 {@link LoginUser} 添加到 {@link WebSocketSession} 中
*
* @author 金灯剑客
*/
public class LoginUserHandshakeInterceptor implements HandshakeInterceptor {
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
WebSocketHandler wsHandler, Map<String, Object> attributes) {
LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
if (loginUser != null) {
WebSocketFrameworkUtils.setLoginUser(loginUser, attributes);
}
return true;
}
@Override
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response,
WebSocketHandler wsHandler, Exception exception) {
// do nothing
}
}

View File

@@ -1,24 +0,0 @@
package com.lideeyunji.tool.framework.websocket.core.security;
import com.lideeyunji.tool.framework.security.config.AuthorizeRequestsCustomizer;
import com.lideeyunji.tool.framework.websocket.config.WebSocketProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
/**
* WebSocket 的权限自定义
*
* @author 金灯剑客
*/
@RequiredArgsConstructor
public class WebSocketAuthorizeRequestsCustomizer extends AuthorizeRequestsCustomizer {
private final WebSocketProperties webSocketProperties;
@Override
public void customize(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry) {
registry.antMatchers(webSocketProperties.getPath()).permitAll();
}
}

View File

@@ -1,104 +0,0 @@
package com.lideeyunji.tool.framework.websocket.core.sender;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.lideeyunji.tool.framework.common.util.json.JsonUtils;
import com.lideeyunji.tool.framework.websocket.core.message.JsonWebSocketMessage;
import com.lideeyunji.tool.framework.websocket.core.session.WebSocketSessionManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* WebSocketMessageSender 实现类
*
* @author 金灯剑客
*/
@Slf4j
@RequiredArgsConstructor
public abstract class AbstractWebSocketMessageSender implements WebSocketMessageSender {
private final WebSocketSessionManager sessionManager;
@Override
public void send(Integer userType, Long userId, String messageType, String messageContent) {
send(null, userType, userId, messageType, messageContent);
}
@Override
public void send(Integer userType, String messageType, String messageContent) {
send(null, userType, null, messageType, messageContent);
}
@Override
public void send(String sessionId, String messageType, String messageContent) {
send(sessionId, null, null, messageType, messageContent);
}
/**
* 发送消息
*
* @param sessionId Session 编号
* @param userType 用户类型
* @param userId 用户编号
* @param messageType 消息类型
* @param messageContent 消息内容
*/
public void send(String sessionId, Integer userType, Long userId, String messageType, String messageContent) {
// 1. 获得 Session 列表
List<WebSocketSession> sessions = Collections.emptyList();
if (StrUtil.isNotEmpty(sessionId)) {
WebSocketSession session = sessionManager.getSession(sessionId);
if (session != null) {
sessions = Collections.singletonList(session);
}
} else if (userType != null && userId != null) {
sessions = (List<WebSocketSession>) sessionManager.getSessionList(userType, userId);
} else if (userType != null) {
sessions = (List<WebSocketSession>) sessionManager.getSessionList(userType);
}
if (CollUtil.isEmpty(sessions)) {
log.info("[send][sessionId({}) userType({}) userId({}) messageType({}) messageContent({}) 未匹配到会话]",
sessionId, userType, userId, messageType, messageContent);
}
// 2. 执行发送
doSend(sessions, messageType, messageContent);
}
/**
* 发送消息的具体实现
*
* @param sessions Session 列表
* @param messageType 消息类型
* @param messageContent 消息内容
*/
public void doSend(Collection<WebSocketSession> sessions, String messageType, String messageContent) {
JsonWebSocketMessage message = new JsonWebSocketMessage().setType(messageType).setContent(messageContent);
String payload = JsonUtils.toJsonString(message); // 关键,使用 JSON 序列化
sessions.forEach(session -> {
// 1. 各种校验,保证 Session 可以被发送
if (session == null) {
log.error("[doSend][session 为空, message({})]", message);
return;
}
if (!session.isOpen()) {
log.error("[doSend][session({}) 已关闭, message({})]", session.getId(), message);
return;
}
// 2. 执行发送
try {
session.sendMessage(new TextMessage(payload));
log.info("[doSend][session({}) 发送消息成功message({})]", session.getId(), message);
} catch (IOException ex) {
log.error("[doSend][session({}) 发送消息失败message({})]", session.getId(), message, ex);
}
});
}
}

View File

@@ -1,52 +0,0 @@
package com.lideeyunji.tool.framework.websocket.core.sender;
import com.lideeyunji.tool.framework.common.util.json.JsonUtils;
/**
* WebSocket 消息的发送器接口
*
* @author 金灯剑客
*/
public interface WebSocketMessageSender {
/**
* 发送消息给指定用户
*
* @param userType 用户类型
* @param userId 用户编号
* @param messageType 消息类型
* @param messageContent 消息内容JSON 格式
*/
void send(Integer userType, Long userId, String messageType, String messageContent);
/**
* 发送消息给指定用户类型
*
* @param userType 用户类型
* @param messageType 消息类型
* @param messageContent 消息内容JSON 格式
*/
void send(Integer userType, String messageType, String messageContent);
/**
* 发送消息给指定 Session
*
* @param sessionId Session 编号
* @param messageType 消息类型
* @param messageContent 消息内容JSON 格式
*/
void send(String sessionId, String messageType, String messageContent);
default void sendObject(Integer userType, Long userId, String messageType, Object messageContent) {
send(userType, userId, messageType, JsonUtils.toJsonString(messageContent));
}
default void sendObject(Integer userType, String messageType, Object messageContent) {
send(userType, messageType, JsonUtils.toJsonString(messageContent));
}
default void sendObject(String sessionId, String messageType, Object messageContent) {
send(sessionId, messageType, JsonUtils.toJsonString(messageContent));
}
}

View File

@@ -1,20 +0,0 @@
package com.lideeyunji.tool.framework.websocket.core.sender.local;
import com.lideeyunji.tool.framework.websocket.core.sender.AbstractWebSocketMessageSender;
import com.lideeyunji.tool.framework.websocket.core.sender.WebSocketMessageSender;
import com.lideeyunji.tool.framework.websocket.core.session.WebSocketSessionManager;
/**
* 本地的 {@link WebSocketMessageSender} 实现类
*
* 注意:仅仅适合单机场景!!!
*
* @author 金灯剑客
*/
public class LocalWebSocketMessageSender extends AbstractWebSocketMessageSender {
public LocalWebSocketMessageSender(WebSocketSessionManager sessionManager) {
super(sessionManager);
}
}

View File

@@ -1,49 +0,0 @@
package com.lideeyunji.tool.framework.websocket.core.session;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator;
import org.springframework.web.socket.handler.WebSocketHandlerDecorator;
/**
* {@link WebSocketHandler} 的装饰类,实现了以下功能:
*
* 1. {@link WebSocketSession} 连接或关闭时,使用 {@link #sessionManager} 进行管理
* 2. 封装 {@link WebSocketSession} 支持并发操作
*
* @author 金灯剑客
*/
public class WebSocketSessionHandlerDecorator extends WebSocketHandlerDecorator {
/**
* 发送时间的限制,单位:毫秒
*/
private static final Integer SEND_TIME_LIMIT = 1000 * 5;
/**
* 发送消息缓冲上线单位bytes
*/
private static final Integer BUFFER_SIZE_LIMIT = 1024 * 100;
private final WebSocketSessionManager sessionManager;
public WebSocketSessionHandlerDecorator(WebSocketHandler delegate,
WebSocketSessionManager sessionManager) {
super(delegate);
this.sessionManager = sessionManager;
}
@Override
public void afterConnectionEstablished(WebSocketSession session) {
// 实现 session 支持并发,可参考 https://blog.csdn.net/abu935009066/article/details/131218149
session = new ConcurrentWebSocketSessionDecorator(session, SEND_TIME_LIMIT, BUFFER_SIZE_LIMIT);
// 添加到 WebSocketSessionManager 中
sessionManager.addSession(session);
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) {
sessionManager.removeSession(session);
}
}

View File

@@ -1,53 +0,0 @@
package com.lideeyunji.tool.framework.websocket.core.session;
import org.springframework.web.socket.WebSocketSession;
import java.util.Collection;
/**
* {@link WebSocketSession} 管理器的接口
*
* @author 金灯剑客
*/
public interface WebSocketSessionManager {
/**
* 添加 Session
*
* @param session Session
*/
void addSession(WebSocketSession session);
/**
* 移除 Session
*
* @param session Session
*/
void removeSession(WebSocketSession session);
/**
* 获得指定编号的 Session
*
* @param id Session 编号
* @return Session
*/
WebSocketSession getSession(String id);
/**
* 获得指定用户类型的 Session 列表
*
* @param userType 用户类型
* @return Session 列表
*/
Collection<WebSocketSession> getSessionList(Integer userType);
/**
* 获得指定用户编号的 Session 列表
*
* @param userType 用户类型
* @param userId 用户编号
* @return Session 列表
*/
Collection<WebSocketSession> getSessionList(Integer userType, Long userId);
}

View File

@@ -1,125 +0,0 @@
package com.lideeyunji.tool.framework.websocket.core.session;
import cn.hutool.core.collection.CollUtil;
import com.lideeyunji.tool.framework.security.core.LoginUser;
import com.lideeyunji.tool.framework.tenant.core.context.TenantContextHolder;
import com.lideeyunji.tool.framework.websocket.core.util.WebSocketFrameworkUtils;
import org.springframework.web.socket.WebSocketSession;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* 默认的 {@link WebSocketSessionManager} 实现类
*
* @author 金灯剑客
*/
public class WebSocketSessionManagerImpl implements WebSocketSessionManager {
/**
* id 与 WebSocketSession 映射
*
* keySession 编号
*/
private final ConcurrentMap<String, WebSocketSession> idSessions = new ConcurrentHashMap<>();
/**
* user 与 WebSocketSession 映射
*
* key1用户类型
* key2用户编号
*/
private final ConcurrentMap<Integer, ConcurrentMap<Long, CopyOnWriteArrayList<WebSocketSession>>> userSessions
= new ConcurrentHashMap<>();
@Override
public void addSession(WebSocketSession session) {
// 添加到 idSessions 中
idSessions.put(session.getId(), session);
// 添加到 userSessions 中
LoginUser user = WebSocketFrameworkUtils.getLoginUser(session);
if (user == null) {
return;
}
ConcurrentMap<Long, CopyOnWriteArrayList<WebSocketSession>> userSessionsMap = userSessions.get(user.getUserType());
if (userSessionsMap == null) {
userSessionsMap = new ConcurrentHashMap<>();
if (userSessions.putIfAbsent(user.getUserType(), userSessionsMap) != null) {
userSessionsMap = userSessions.get(user.getUserType());
}
}
CopyOnWriteArrayList<WebSocketSession> sessions = userSessionsMap.get(user.getId());
if (sessions == null) {
sessions = new CopyOnWriteArrayList<>();
if (userSessionsMap.putIfAbsent(user.getId(), sessions) != null) {
sessions = userSessionsMap.get(user.getId());
}
}
sessions.add(session);
}
@Override
public void removeSession(WebSocketSession session) {
// 移除从 idSessions 中
idSessions.remove(session.getId());
// 移除从 idSessions 中
LoginUser user = WebSocketFrameworkUtils.getLoginUser(session);
if (user == null) {
return;
}
ConcurrentMap<Long, CopyOnWriteArrayList<WebSocketSession>> userSessionsMap = userSessions.get(user.getUserType());
if (userSessionsMap == null) {
return;
}
CopyOnWriteArrayList<WebSocketSession> sessions = userSessionsMap.get(user.getId());
sessions.removeIf(session0 -> session0.getId().equals(session.getId()));
if (CollUtil.isEmpty(sessions)) {
userSessionsMap.remove(user.getId(), sessions);
}
}
@Override
public WebSocketSession getSession(String id) {
return idSessions.get(id);
}
@Override
public Collection<WebSocketSession> getSessionList(Integer userType) {
ConcurrentMap<Long, CopyOnWriteArrayList<WebSocketSession>> userSessionsMap = userSessions.get(userType);
if (CollUtil.isEmpty(userSessionsMap)) {
return new ArrayList<>();
}
LinkedList<WebSocketSession> result = new LinkedList<>(); // 避免扩容
Long contextTenantId = TenantContextHolder.getTenantId();
for (List<WebSocketSession> sessions : userSessionsMap.values()) {
if (CollUtil.isEmpty(sessions)) {
continue;
}
// 特殊:如果租户不匹配,则直接排除
if (contextTenantId != null) {
Long userTenantId = WebSocketFrameworkUtils.getTenantId(sessions.get(0));
if (!contextTenantId.equals(userTenantId)) {
continue;
}
}
result.addAll(sessions);
}
return result;
}
@Override
public Collection<WebSocketSession> getSessionList(Integer userType, Long userId) {
ConcurrentMap<Long, CopyOnWriteArrayList<WebSocketSession>> userSessionsMap = userSessions.get(userType);
if (CollUtil.isEmpty(userSessionsMap)) {
return new ArrayList<>();
}
CopyOnWriteArrayList<WebSocketSession> sessions = userSessionsMap.get(userId);
return CollUtil.isNotEmpty(sessions) ? new ArrayList<>(sessions) : new ArrayList<>();
}
}

View File

@@ -1,67 +0,0 @@
package com.lideeyunji.tool.framework.websocket.core.util;
import com.lideeyunji.tool.framework.security.core.LoginUser;
import org.springframework.web.socket.WebSocketSession;
import java.util.Map;
/**
* 专属于 web 包的工具类
*
* @author 金灯剑客
*/
public class WebSocketFrameworkUtils {
public static final String ATTRIBUTE_LOGIN_USER = "LOGIN_USER";
/**
* 设置当前用户
*
* @param loginUser 登录用户
* @param attributes Session
*/
public static void setLoginUser(LoginUser loginUser, Map<String, Object> attributes) {
attributes.put(ATTRIBUTE_LOGIN_USER, loginUser);
}
/**
* 获取当前用户
*
* @return 当前用户
*/
public static LoginUser getLoginUser(WebSocketSession session) {
return (LoginUser) session.getAttributes().get(ATTRIBUTE_LOGIN_USER);
}
/**
* 获得当前用户的编号
*
* @return 用户编号
*/
public static Long getLoginUserId(WebSocketSession session) {
LoginUser loginUser = getLoginUser(session);
return loginUser != null ? loginUser.getId() : null;
}
/**
* 获得当前用户的类型
*
* @return 用户编号
*/
public static Integer getLoginUserType(WebSocketSession session) {
LoginUser loginUser = getLoginUser(session);
return loginUser != null ? loginUser.getUserType() : null;
}
/**
* 获得当前用户的租户编号
*
* @param session Session
* @return 租户编号
*/
public static Long getTenantId(WebSocketSession session) {
LoginUser loginUser = getLoginUser(session);
return loginUser != null ? loginUser.getTenantId() : null;
}
}

View File

@@ -1,4 +0,0 @@
/**
* WebSocket 框架,支持多节点的广播
*/
package com.lideeyunji.tool.framework.websocket;

View File

@@ -1 +0,0 @@
com.lideeyunji.tool.framework.websocket.config.WebSocketAutoConfiguration

View File

@@ -1,26 +0,0 @@
{
"groups": [
{
"name": "lideeyunji.websocket",
"type": "com.lideeyunji.tool.framework.websocket.config.WebSocketProperties",
"sourceType": "com.lideeyunji.tool.framework.websocket.config.WebSocketProperties"
}
],
"properties": [
{
"name": "lideeyunji.websocket.path",
"type": "java.lang.String",
"description": "WebSocket 的连接路径",
"sourceType": "com.lideeyunji.tool.framework.websocket.config.WebSocketProperties",
"defaultValue": "\/ws"
},
{
"name": "lideeyunji.websocket.sender-type",
"type": "java.lang.String",
"description": "消息发送器的类型 可选值local、redis、rocketmq、kafka、rabbitmq",
"sourceType": "com.lideeyunji.tool.framework.websocket.config.WebSocketProperties",
"defaultValue": "local"
}
],
"hints": []
}