mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
update:优化
This commit is contained in:
parent
a5ca37935c
commit
ac23d4ab82
2 changed files with 45 additions and 3 deletions
|
|
@ -65,8 +65,8 @@ public class AgentMcpAccessPointServiceImpl implements AgentMcpAccessPointServic
|
|||
try (WebSocketClientManager client = WebSocketClientManager.build(
|
||||
new WebSocketClientManager.Builder()
|
||||
.uri(wsUrl)
|
||||
.connectTimeout(5, TimeUnit.SECONDS)
|
||||
.maxSessionDuration(15, TimeUnit.SECONDS))) {
|
||||
.connectTimeout(8, TimeUnit.SECONDS)
|
||||
.maxSessionDuration(10, TimeUnit.SECONDS))) {
|
||||
|
||||
// 步骤1: 发送初始化消息并等待响应
|
||||
log.info("发送MCP初始化消息,智能体ID: {}", id);
|
||||
|
|
@ -83,7 +83,7 @@ public class AgentMcpAccessPointServiceImpl implements AgentMcpAccessPointServic
|
|||
client.sendJson(initializeRequest);
|
||||
|
||||
// 等待初始化响应 (id=1) - 移除固定延迟,改为响应驱动
|
||||
List<String> initResponses = client.listener(response -> {
|
||||
List<String> initResponses = client.listenerWithoutClose(response -> {
|
||||
try {
|
||||
Map<String, Object> jsonMap = JsonUtils.parseObject(response, Map.class);
|
||||
if (jsonMap != null && Integer.valueOf(1).equals(jsonMap.get("id"))) {
|
||||
|
|
|
|||
|
|
@ -137,6 +137,37 @@ public class WebSocketClientManager implements Closeable {
|
|||
return collected;
|
||||
}
|
||||
|
||||
private <T> List<T> listenerCustomWithoutClose(
|
||||
BlockingQueue<T> queue,
|
||||
Predicate<T> predicate)
|
||||
throws InterruptedException, TimeoutException, ExecutionException {
|
||||
List<T> collected = new ArrayList<>();
|
||||
long deadline = System.currentTimeMillis() + maxSessionDurationUnit.toMillis(maxSessionDuration);
|
||||
|
||||
while (true) {
|
||||
if (errorFuture.isDone()) {
|
||||
errorFuture.get();
|
||||
}
|
||||
|
||||
long remaining = deadline - System.currentTimeMillis();
|
||||
if (remaining <= 0) {
|
||||
throw new TimeoutException("等待批量消息超时");
|
||||
}
|
||||
|
||||
T msg = queue.poll(remaining, TimeUnit.MILLISECONDS);
|
||||
if (msg == null) {
|
||||
throw new TimeoutException("等待批量消息超时");
|
||||
}
|
||||
|
||||
collected.add(msg);
|
||||
if (predicate.test(msg)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// 不调用 close(),保持连接开放
|
||||
return collected;
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步接收多条消息,直到 predicate 为 true 或超时抛异常;
|
||||
*
|
||||
|
|
@ -147,6 +178,17 @@ public class WebSocketClientManager implements Closeable {
|
|||
return listenerCustom(textMessageQueue, predicate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步接收多条消息,直到 predicate 为 true 或超时抛异常;
|
||||
* 不自动关闭连接,适用于需要在同一连接上发送多个消息的场景
|
||||
*
|
||||
* @return 返回监听期间的所有消息列表
|
||||
*/
|
||||
public List<String> listenerWithoutClose(Predicate<String> predicate)
|
||||
throws InterruptedException, TimeoutException, ExecutionException {
|
||||
return listenerCustomWithoutClose(textMessageQueue, predicate);
|
||||
}
|
||||
|
||||
public List<byte[]> listenerBinary(Predicate<byte[]> predicate)
|
||||
throws InterruptedException, TimeoutException, ExecutionException {
|
||||
return listenerCustom(binaryMessageQueue, predicate);
|
||||
|
|
|
|||
Loading…
Reference in a new issue