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
*
* 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();
// }
//}

View File

@ -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处理器配置端点和允许的源

View File

@ -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<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;
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<String, String> 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();
}
/**

View File

@ -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: