diff --git a/eladmin/eladmin-common/src/main/java/me/zhengjie/config/webConfig/WebSocketConfig.java b/eladmin/eladmin-common/src/main/java/me/zhengjie/config/webConfig/WebSocketConfig.java index 67450cf..c298bd1 100644 --- a/eladmin/eladmin-common/src/main/java/me/zhengjie/config/webConfig/WebSocketConfig.java +++ b/eladmin/eladmin-common/src/main/java/me/zhengjie/config/webConfig/WebSocketConfig.java @@ -1,33 +1,33 @@ -/* - * Copyright 2019-2025 Tz - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package me.zhengjie.config.webConfig; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.socket.server.standard.ServerEndpointExporter; - -/** - * @author Tz - * @date 2019-08-24 15:44 - */ -@Configuration -public class WebSocketConfig { - - @Bean - public ServerEndpointExporter serverEndpointExporter() { - return new ServerEndpointExporter(); - } -} +///* +// * Copyright 2019-2025 Tz +// * +// * Licensed under the Apache License, Version 2.0 (the "License"); +// * you may not use this file except in compliance with the License. +// * You may obtain a copy of the License at +// * +// * http://www.apache.org/licenses/LICENSE-2.0 +// * +// * Unless required by applicable law or agreed to in writing, software +// * distributed under the License is distributed on an "AS IS" BASIS, +// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// * See the License for the specific language governing permissions and +// * limitations under the License. +// */ +//package me.zhengjie.config.webConfig; +// +//import org.springframework.context.annotation.Bean; +//import org.springframework.context.annotation.Configuration; +//import org.springframework.web.socket.server.standard.ServerEndpointExporter; +// +///** +// * @author Tz +// * @date 2019-08-24 15:44 +// */ +//@Configuration +//public class WebSocketConfig { +// +// @Bean +// public ServerEndpointExporter serverEndpointExporter() { +// return new ServerEndpointExporter(); +// } +//} diff --git a/eladmin/eladmin-system/src/main/java/me/zhengjie/modules/security/config/WebSocketConfig.java b/eladmin/eladmin-system/src/main/java/me/zhengjie/modules/security/config/WebSocketConfig.java index 5d21fdd..628d45c 100644 --- a/eladmin/eladmin-system/src/main/java/me/zhengjie/modules/security/config/WebSocketConfig.java +++ b/eladmin/eladmin-system/src/main/java/me/zhengjie/modules/security/config/WebSocketConfig.java @@ -1,6 +1,7 @@ package me.zhengjie.modules.security.config; import me.zhengjie.modules.security.config.handler.DeviceWebSocketHandler; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.config.annotation.EnableWebSocket; @@ -16,11 +17,8 @@ import org.springframework.web.socket.server.standard.ServletServerContainerFact @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { - private final DeviceWebSocketHandler deviceWebSocketHandler; - - public WebSocketConfig(DeviceWebSocketHandler deviceWebSocketHandler) { - this.deviceWebSocketHandler = deviceWebSocketHandler; - } + @Autowired + private DeviceWebSocketHandler deviceWebSocketHandler; /** * 注册WebSocket处理器,配置端点和允许的源 diff --git a/eladmin/eladmin-system/src/main/java/me/zhengjie/modules/security/config/handler/DeviceWebSocketHandler.java b/eladmin/eladmin-system/src/main/java/me/zhengjie/modules/security/config/handler/DeviceWebSocketHandler.java index 530ebeb..56ac269 100644 --- a/eladmin/eladmin-system/src/main/java/me/zhengjie/modules/security/config/handler/DeviceWebSocketHandler.java +++ b/eladmin/eladmin-system/src/main/java/me/zhengjie/modules/security/config/handler/DeviceWebSocketHandler.java @@ -1,5 +1,7 @@ package me.zhengjie.modules.security.config.handler; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.TextMessage; @@ -16,13 +18,11 @@ import java.util.concurrent.ConcurrentHashMap; @Component public class DeviceWebSocketHandler extends TextWebSocketHandler { + @Autowired @Lazy private + ThirdPartyWebSocketClient thirdPartyClient; + // 存储所有活跃的WebSocket会话,键为会话ID private final ConcurrentHashMap sessions = new ConcurrentHashMap<>(); - private final ThirdPartyWebSocketClient thirdPartyClient; - - public DeviceWebSocketHandler(ThirdPartyWebSocketClient thirdPartyClient) { - this.thirdPartyClient = thirdPartyClient; - } /** * 处理来自客户端的文本消息 diff --git a/eladmin/eladmin-system/src/main/java/me/zhengjie/modules/security/config/handler/ThirdPartyWebSocketClient.java b/eladmin/eladmin-system/src/main/java/me/zhengjie/modules/security/config/handler/ThirdPartyWebSocketClient.java index 5b7f32d..1b9287a 100644 --- a/eladmin/eladmin-system/src/main/java/me/zhengjie/modules/security/config/handler/ThirdPartyWebSocketClient.java +++ b/eladmin/eladmin-system/src/main/java/me/zhengjie/modules/security/config/handler/ThirdPartyWebSocketClient.java @@ -1,6 +1,8 @@ package me.zhengjie.modules.security.config.handler; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; import org.springframework.web.socket.client.WebSocketClient; import org.springframework.web.socket.client.standard.StandardWebSocketClient; @@ -25,22 +27,20 @@ public class ThirdPartyWebSocketClient extends TextWebSocketHandler { @Value("${thirdparty.ws.url}") private String thirdPartyWsUrl; + @Autowired @Lazy + private DeviceWebSocketHandler deviceHandler; + private WebSocketSession session; // 存储已订阅的设备,键为设备ID,值为订阅状态 private final ConcurrentHashMap subscribedDevices = new ConcurrentHashMap<>(); - private final DeviceWebSocketHandler deviceHandler; private final ScheduledExecutorService reconnectExecutor = Executors.newSingleThreadScheduledExecutor(); - public ThirdPartyWebSocketClient(DeviceWebSocketHandler deviceHandler) { - this.deviceHandler = deviceHandler; - } - /** * Bean初始化后连接到第三方WebSocket服务 */ @PostConstruct public void init() { - connect(); + // connect(); } /** diff --git a/eladmin/eladmin-system/src/main/resources/config/application-dev.yml b/eladmin/eladmin-system/src/main/resources/config/application-dev.yml index 51b6575..8794683 100644 --- a/eladmin/eladmin-system/src/main/resources/config/application-dev.yml +++ b/eladmin/eladmin-system/src/main/resources/config/application-dev.yml @@ -110,7 +110,7 @@ weChat: # socket地址 thirdparty: ws: - url: ws://127.0.0.1:3030/websocket + url: ws://127.0.0.1:8000/websocket # 文件存储路径 file: