This commit is contained in:
tangzh 2025-05-26 23:19:06 +08:00
parent fb4fe13ab4
commit 442c759216
5 changed files with 48 additions and 50 deletions

View File

@ -1,33 +1,33 @@
/* ///*
* Copyright 2019-2025 Tz // * Copyright 2019-2025 Tz
* // *
* Licensed under the Apache License, Version 2.0 (the "License"); // * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. // * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at // * You may obtain a copy of the License at
* // *
* http://www.apache.org/licenses/LICENSE-2.0 // * http://www.apache.org/licenses/LICENSE-2.0
* // *
* Unless required by applicable law or agreed to in writing, software // * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, // * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and // * See the License for the specific language governing permissions and
* limitations under the License. // * limitations under the License.
*/ // */
package me.zhengjie.config.webConfig; //package me.zhengjie.config.webConfig;
//
import org.springframework.context.annotation.Bean; //import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; //import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter; //import org.springframework.web.socket.server.standard.ServerEndpointExporter;
//
/** ///**
* @author Tz // * @author Tz
* @date 2019-08-24 15:44 // * @date 2019-08-24 15:44
*/ // */
@Configuration //@Configuration
public class WebSocketConfig { //public class WebSocketConfig {
//
@Bean // @Bean
public ServerEndpointExporter serverEndpointExporter() { // public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter(); // return new ServerEndpointExporter();
} // }
} //}

View File

@ -1,6 +1,7 @@
package me.zhengjie.modules.security.config; package me.zhengjie.modules.security.config;
import me.zhengjie.modules.security.config.handler.DeviceWebSocketHandler; 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket; import org.springframework.web.socket.config.annotation.EnableWebSocket;
@ -16,11 +17,8 @@ import org.springframework.web.socket.server.standard.ServletServerContainerFact
@EnableWebSocket @EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer { public class WebSocketConfig implements WebSocketConfigurer {
private final DeviceWebSocketHandler deviceWebSocketHandler; @Autowired
private DeviceWebSocketHandler deviceWebSocketHandler;
public WebSocketConfig(DeviceWebSocketHandler deviceWebSocketHandler) {
this.deviceWebSocketHandler = deviceWebSocketHandler;
}
/** /**
* 注册WebSocket处理器配置端点和允许的源 * 注册WebSocket处理器配置端点和允许的源

View File

@ -1,5 +1,7 @@
package me.zhengjie.modules.security.config.handler; 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.stereotype.Component;
import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.TextMessage;
@ -16,13 +18,11 @@ import java.util.concurrent.ConcurrentHashMap;
@Component @Component
public class DeviceWebSocketHandler extends TextWebSocketHandler { public class DeviceWebSocketHandler extends TextWebSocketHandler {
@Autowired @Lazy private
ThirdPartyWebSocketClient thirdPartyClient;
// 存储所有活跃的WebSocket会话键为会话ID // 存储所有活跃的WebSocket会话键为会话ID
private final ConcurrentHashMap<String, WebSocketSession> sessions = new ConcurrentHashMap<>(); private final ConcurrentHashMap<String, WebSocketSession> sessions = new ConcurrentHashMap<>();
private final ThirdPartyWebSocketClient thirdPartyClient;
public DeviceWebSocketHandler(ThirdPartyWebSocketClient thirdPartyClient) {
this.thirdPartyClient = thirdPartyClient;
}
/** /**
* 处理来自客户端的文本消息 * 处理来自客户端的文本消息

View File

@ -1,6 +1,8 @@
package me.zhengjie.modules.security.config.handler; package me.zhengjie.modules.security.config.handler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.socket.client.WebSocketClient; import org.springframework.web.socket.client.WebSocketClient;
import org.springframework.web.socket.client.standard.StandardWebSocketClient; import org.springframework.web.socket.client.standard.StandardWebSocketClient;
@ -25,22 +27,20 @@ public class ThirdPartyWebSocketClient extends TextWebSocketHandler {
@Value("${thirdparty.ws.url}") @Value("${thirdparty.ws.url}")
private String thirdPartyWsUrl; private String thirdPartyWsUrl;
@Autowired @Lazy
private DeviceWebSocketHandler deviceHandler;
private WebSocketSession session; private WebSocketSession session;
// 存储已订阅的设备键为设备ID值为订阅状态 // 存储已订阅的设备键为设备ID值为订阅状态
private final ConcurrentHashMap<String, String> subscribedDevices = new ConcurrentHashMap<>(); private final ConcurrentHashMap<String, String> subscribedDevices = new ConcurrentHashMap<>();
private final DeviceWebSocketHandler deviceHandler;
private final ScheduledExecutorService reconnectExecutor = Executors.newSingleThreadScheduledExecutor(); private final ScheduledExecutorService reconnectExecutor = Executors.newSingleThreadScheduledExecutor();
public ThirdPartyWebSocketClient(DeviceWebSocketHandler deviceHandler) {
this.deviceHandler = deviceHandler;
}
/** /**
* Bean初始化后连接到第三方WebSocket服务 * Bean初始化后连接到第三方WebSocket服务
*/ */
@PostConstruct @PostConstruct
public void init() { public void init() {
connect(); // connect();
} }
/** /**

View File

@ -110,7 +110,7 @@ weChat:
# socket地址 # socket地址
thirdparty: thirdparty:
ws: ws:
url: ws://127.0.0.1:3030/websocket url: ws://127.0.0.1:8000/websocket
# 文件存储路径 # 文件存储路径
file: file: