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