refactor(websocket): 优化 WebSocket 配置和消息处理逻辑

- 在 RedisConfiguration 中移除 CachingConfigurerSupport 继承并调整方法重写
- 为 KitWsServer 添加空消息处理逻辑,当收到 "[{}]" 消息时清理客户端连接
- 在 KitSpringBeanHolder 中将线程ID获取方式从 getId() 改为 threadId()
- 为 Redis 扫描相关方法添加 SuppressWarnings 注解以处理泛型警告
This commit is contained in:
2026-02-25 17:55:21 +08:00
parent a78b1d9e08
commit 53cba0c887
8 changed files with 31 additions and 10 deletions
@@ -162,7 +162,7 @@ public class KitSpringBeanHolder implements ApplicationContextAware, DisposableB
* @return /
*/
default String getCallBackName() {
return Thread.currentThread().getId() + ":" + this.getClass().getName();
return Thread.currentThread().threadId() + ":" + this.getClass().getName();
}
}
}
@@ -106,6 +106,7 @@ public class KitRedisHelper {
* @param pattern key
* @return /
*/
@SuppressWarnings("all")
public List<String> scan(String pattern) {
ScanOptions options = ScanOptions.scanOptions().match(pattern).build();
RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
@@ -133,6 +134,7 @@ public class KitRedisHelper {
* @param size 每页数目
* @return /
*/
@SuppressWarnings("all")
public List<String> findKeysForPage(String patternKey, int page, int size) {
ScanOptions options = ScanOptions.scanOptions().match(patternKey).build();
RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
@@ -207,12 +209,12 @@ public class KitRedisHelper {
*
* @param pattern /
*/
@SuppressWarnings("all")
public void scanDel(String pattern) {
ScanOptions options = ScanOptions.scanOptions().match(pattern).count(100).build();
try (Cursor<byte[]> cursor = redisTemplate.executeWithStickyConnection(connection -> {
try {
return (Cursor<byte[]>) new ConvertingCursor<>(
connection.scan(options), redisTemplate.getKeySerializer()::deserialize);
return (Cursor<byte[]>) new ConvertingCursor<>(connection.scan(options), redisTemplate.getKeySerializer()::deserialize);
} catch (Exception e) {
throw new RuntimeException("Redis scan operation failed", e);
}
@@ -26,7 +26,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.KeyGenerator;
@@ -50,7 +49,7 @@ import java.util.Map;
@Configuration
@EnableCaching
@AutoConfigureBefore(RedisAutoConfiguration.class)
public class RedisConfiguration extends CachingConfigurerSupport {
public class RedisConfiguration {
@Autowired
private AppProperties properties;
@@ -103,7 +102,6 @@ public class RedisConfiguration extends CachingConfigurerSupport {
* 自定义缓存key生成策略
*/
@Bean
@Override
public KeyGenerator keyGenerator() {
return (target, method, params) -> {
Map<String, Object> container = new HashMap<>(8);
@@ -22,6 +22,8 @@ package cn.odboy.framework.websocket;
*/
public interface KitWsBizCodeConst {
String AutomaticPush = "100"; // 主动推送
String IM = "101"; // 实时聊天
// 主动推送
String AutomaticPush = "100";
// 实时聊天
String IM = "101";
}
@@ -61,6 +61,14 @@ public class KitWsServer {
*/
@OnMessage
public void onMessage(String message, Session session) throws IOException {
if ("[{}]".equals(message)){
try {
KitWsClientManager.removeClient(this.sid);
} catch (Exception e) {
log.error("WebSocket onClose error, sid={}", this.sid, e);
}
return;
}
KitWsMessage wsMessage = JSON.parseObject(message, KitWsMessage.class);
// String bizCode = wsMessage.getBizCode();
// Object data = wsMessage.getData();