异步模式
使用 async/await、协程、链式调用、进度跟踪、取消和流来消费 IConvaiOperation<T> 和 IConvaiStream<T>。
异步/await
using Convai.Runtime.Core.Async;
using Convai.Runtime.Facades;
using System.Threading;
using UnityEngine;
public class AsyncConnectExample : MonoBehaviour
{
private async void Start()
{
var manager = ConvaiManager.ActiveManager;
if (manager == null) return;
try
{
var session = await manager.ConnectAsync(destroyCancellationToken);
Debug.Log($"已连接。房间:{session.RoomId}");
}
catch (ConvaiOperationException ex)
{
Debug.LogError($"[{ex.Code}] 连接失败:{ex.Message}");
}
catch (OperationCanceledException)
{
Debug.Log("连接已取消——场景已卸载。");
}
}
}await operation 与 await operation.AsTask()
await operation 与 await operation.AsTask()协程
ContinueWith 链式调用
进度跟踪
取消
使用 CancellationToken
CancellationToken使用 destroyCancellationToken
destroyCancellationToken使用 operation.Cancel()
operation.Cancel()CancellationToken 与 operation.Cancel()
CancellationToken 与 operation.Cancel()流
错误处理决策表
场景
模式
原因
使用示例
示例 1——带进度条和取消按钮的加载遮罩
示例 2——将转录令牌流式传输到自定义日志控件
故障排查
症状
可能原因
修复方法
下一步
最后更新于
这有帮助吗?