|
@@ -0,0 +1,51 @@
|
|
|
+package com.backendsys.modules.sdk.comfyui.service.impl;
|
|
|
+
|
|
|
+import com.backendsys.modules.sdk.comfyui.service.ComfyUIService;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.reactive.socket.WebSocketMessage;
|
|
|
+import org.springframework.web.reactive.socket.WebSocketSession;
|
|
|
+import org.springframework.web.socket.client.WebSocketClient;
|
|
|
+import org.springframework.web.socket.client.standard.StandardWebSocketClient;
|
|
|
+import reactor.core.publisher.Mono;
|
|
|
+
|
|
|
+import java.net.URI;
|
|
|
+import java.net.URISyntaxException;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+
|
|
|
+import static org.springframework.core.io.support.SpringFactoriesLoader.FailureHandler.handleMessage;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class ComfyUIServiceImpl implements ComfyUIService {
|
|
|
+
|
|
|
+ // 单例 WebSocketClient(线程安全)
|
|
|
+ private final WebSocketClient webSocketClient = new StandardWebSocketClient();
|
|
|
+
|
|
|
+ // 管理多个连接
|
|
|
+ private final Map<String, WebSocketSession> sessions = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+// public Mono<Void> connect(String clientId, String wsUrl) throws URISyntaxException {
|
|
|
+// return webSocketClient.execute(new URI(wsUrl), session -> {
|
|
|
+// // 在 WebSocketSession 的回调中处理消息
|
|
|
+// session.receive() // 返回 Flux<WebSocketMessage>
|
|
|
+// .doOnNext(message -> {
|
|
|
+// // 处理每条消息
|
|
|
+// String payload = message.getPayloadAsText();
|
|
|
+// System.out.println("Received: " + payload);
|
|
|
+// })
|
|
|
+// .doOnError(e -> System.err.println("Error: " + e.getMessage()))
|
|
|
+// .doFinally(signal -> System.out.println("Connection closed: " + signal))
|
|
|
+// .then(); // 返回 Mono<Void> 表示完成
|
|
|
+// });
|
|
|
+// }
|
|
|
+
|
|
|
+ private void handleMessage(String clientId, WebSocketMessage message) {
|
|
|
+ // 转发到 SSE 或其他逻辑
|
|
|
+ System.out.println("Received from " + clientId + ": " + message.getPayloadAsText());
|
|
|
+ }
|
|
|
+
|
|
|
+ private void handleError(String clientId, Throwable e) {
|
|
|
+ System.err.println("Error for " + clientId + ": " + e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+}
|