From 4d438418e4b0e8a302773bae1107013599b85400 Mon Sep 17 00:00:00 2001 From: Hamm Date: Mon, 27 May 2024 17:35:01 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(Bug):=20=E4=BC=98=E5=8C=96=E4=BA=86?= =?UTF-8?q?=E4=B8=80=E4=BA=9B=E5=B7=B2=E7=9F=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../airpower/websocket/WebSocketConfig.java | 17 ++++++++++++++++- .../airpower/websocket/WebsocketHandler.java | 5 +++-- .../hamm/airpower/websocket/WebsocketUtil.java | 8 ++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/airpower-core/src/main/java/cn/hamm/airpower/websocket/WebSocketConfig.java b/airpower-core/src/main/java/cn/hamm/airpower/websocket/WebSocketConfig.java index e147167..4dd47e2 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/websocket/WebSocketConfig.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/websocket/WebSocketConfig.java @@ -1,21 +1,27 @@ package cn.hamm.airpower.websocket; +import cn.hamm.airpower.exception.ServiceException; import lombok.Data; import lombok.experimental.Accessors; +import lombok.extern.slf4j.Slf4j; import org.jetbrains.annotations.NotNull; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; import org.springframework.web.socket.config.annotation.WebSocketConfigurer; import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; import org.springframework.web.socket.handler.TextWebSocketHandler; +import java.util.Objects; + /** *

WebSocket配置

* * @author Hamm */ +@Slf4j @Component @Data @Accessors(chain = true) @@ -42,6 +48,11 @@ public class WebSocketConfig implements WebSocketConfigurer { */ private WebSocketSupport support = WebSocketSupport.NO; + /** + *

发布订阅的频道前缀

+ */ + private String channelPrefix; + @Bean public TextWebSocketHandler getWebSocketHandler() { return new WebsocketHandler(); @@ -55,7 +66,11 @@ public class WebSocketConfig implements WebSocketConfigurer { @Override public void registerWebSocketHandlers(@NotNull WebSocketHandlerRegistry registry) { if (!support.equals(WebSocketSupport.NO)) { - registry.addHandler(getWebSocketHandler(), path).setAllowedOrigins("*"); + return; + } + if (Objects.isNull(channelPrefix) || !StringUtils.hasText(channelPrefix)) { + throw new ServiceException("没有配置 airpower.websocket.channelPrefix, 无法启动WebSocket服务"); } + registry.addHandler(getWebSocketHandler(), path).setAllowedOrigins("*"); } } diff --git a/airpower-core/src/main/java/cn/hamm/airpower/websocket/WebsocketHandler.java b/airpower-core/src/main/java/cn/hamm/airpower/websocket/WebsocketHandler.java index f594294..cef57bd 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/websocket/WebsocketHandler.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/websocket/WebsocketHandler.java @@ -1,6 +1,7 @@ package cn.hamm.airpower.websocket; import cn.hamm.airpower.config.Configs; +import cn.hamm.airpower.config.Constant; import cn.hamm.airpower.exception.ServiceException; import cn.hamm.airpower.model.Json; import cn.hamm.airpower.util.Utils; @@ -115,14 +116,14 @@ public class WebsocketHandler extends TextWebSocketHandler implements MessageLis * @param userId 用户ID */ private void startRedisListener(@NotNull WebSocketSession session, long userId) { - final String personalChannel = CHANNEL_USER_PREFIX + userId; + final String personalChannel = Configs.getWebsocketConfig().getChannelPrefix() + Constant.UNDERLINE + CHANNEL_USER_PREFIX + userId; redisConnectionFactory.getConnection().subscribe( (message, pattern) -> { synchronized (session) { onChannelMessage(new String(message.getBody(), StandardCharsets.UTF_8), session); } }, - CHANNEL_ALL.getBytes(StandardCharsets.UTF_8), + (Configs.getWebsocketConfig().getChannelPrefix() + Constant.UNDERLINE + CHANNEL_ALL).getBytes(StandardCharsets.UTF_8), personalChannel.getBytes(StandardCharsets.UTF_8) ); } diff --git a/airpower-core/src/main/java/cn/hamm/airpower/websocket/WebsocketUtil.java b/airpower-core/src/main/java/cn/hamm/airpower/websocket/WebsocketUtil.java index 76e5564..d0b652d 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/websocket/WebsocketUtil.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/websocket/WebsocketUtil.java @@ -1,12 +1,16 @@ package cn.hamm.airpower.websocket; import cn.hamm.airpower.config.Configs; +import cn.hamm.airpower.exception.ServiceException; import cn.hamm.airpower.model.Json; import cn.hamm.airpower.util.Utils; import lombok.extern.slf4j.Slf4j; import org.eclipse.paho.client.mqttv3.MqttException; import org.jetbrains.annotations.NotNull; import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; + +import java.util.Objects; /** *

WebsocketUtil

@@ -42,6 +46,10 @@ public class WebsocketUtil { * @param message 消息 */ private @NotNull WebSocketEvent publish(String channel, WebSocketMessage message) { + final String channelPrefix = Configs.getWebsocketConfig().getChannelPrefix(); + if (Objects.isNull(channelPrefix) || !StringUtils.hasText(channelPrefix)) { + throw new ServiceException("没有配置 airpower.websocket.channelPrefix, 无法启动WebSocket服务"); + } WebSocketEvent webSocketEvent = new WebSocketEvent<>(); webSocketEvent.setData(message); switch (Configs.getWebsocketConfig().getSupport()) { -- Gitee From 7f470b360cfac5f3c3607e42746edabbd038727c Mon Sep 17 00:00:00 2001 From: Hamm Date: Mon, 27 May 2024 17:35:48 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix(Bug):=20=E4=BC=98=E5=8C=96=E4=BA=86?= =?UTF-8?q?=E4=B8=80=E4=BA=9B=E5=B7=B2=E7=9F=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/hamm/airpower/websocket/WebSocketConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/airpower-core/src/main/java/cn/hamm/airpower/websocket/WebSocketConfig.java b/airpower-core/src/main/java/cn/hamm/airpower/websocket/WebSocketConfig.java index 4dd47e2..b96bddb 100644 --- a/airpower-core/src/main/java/cn/hamm/airpower/websocket/WebSocketConfig.java +++ b/airpower-core/src/main/java/cn/hamm/airpower/websocket/WebSocketConfig.java @@ -65,7 +65,7 @@ public class WebSocketConfig implements WebSocketConfigurer { */ @Override public void registerWebSocketHandlers(@NotNull WebSocketHandlerRegistry registry) { - if (!support.equals(WebSocketSupport.NO)) { + if (support.equals(WebSocketSupport.NO)) { return; } if (Objects.isNull(channelPrefix) || !StringUtils.hasText(channelPrefix)) { -- Gitee