You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CAF接收器队列实现疑问及HTTP服务管理示例求助

解答:Google CAF Receiver队列的异步实现与HTTP服务示例

首先得给你点个赞,你发现的官方文档命名不一致问题确实很坑——cast.framework.QueueBase和错误的cast.framework.messages.QueueBase混用,尤其是文档链接错误很容易误导人。不过好消息是,正确的cast.framework.QueueBase类文档明确说明了fetchItems等方法支持返回Promise(或者包含QueueItem数组的Promise),这意味着你完全可以用异步HTTP请求来从后端管理队列,不用被文档里看似同步的示例限制。

下面直接给你一个用HTTP服务管理队列的可运行示例:

class HttpBackedQueue extends cast.framework.QueueBase {
  // 从后端获取完整队列
  fetchItems(context) {
    // 替换成你的实际后端接口
    return fetch('/api/cast/queue')
      .then(res => {
        if (!res.ok) throw new Error(`请求失败:${res.status}`);
        return res.json();
      })
      .then(rawQueue => {
        // 把后端返回的数据映射成Cast要求的QueueItem格式
        return rawQueue.map(item => ({
          itemId: item.id.toString(),
          media: {
            contentId: item.streamUrl,
            contentType: item.mimeType || 'video/mp4',
            metadata: new cast.framework.messages.GenericMediaMetadata({
              title: item.title,
              images: [{ url: item.posterUrl }]
            })
          }
        }));
      })
      .catch(err => {
        console.error('加载队列失败:', err);
        // 兜底返回默认队列(可选)
        return [{
          media: { contentId: 'https://example.com/fallback.mp4', contentType: 'video/mp4' }
        }];
      });
  }

  // 可选:从后端获取下一个队列项(比如队列是动态的场景)
  nextItem(context, currentItem) {
    return fetch(`/api/cast/queue/next/${currentItem.itemId}`)
      .then(res => res.json())
      .then(nextRaw => ({
        itemId: nextRaw.id.toString(),
        media: {
          contentId: nextRaw.streamUrl,
          contentType: nextRaw.mimeType
        }
      }));
  }
}

// 在Receiver初始化时注册这个队列
const receiverContext = cast.framework.CastReceiverContext.getInstance();
receiverContext.setQueue(new HttpBackedQueue());
receiverContext.start();

关键说明

官方文档里对fetchItems返回值的定义其实写得很清楚:

(non-null Array of non-null cast.framework.messages.QueueItem or non-null Promise containing non-null Array of non-null cast.framework.messages.QueueItem)

也就是说同步返回数组或者异步返回Promise都是合法的,文档里的示例只是为了简洁用了同步场景,实际开发中完全可以用HTTP请求这种异步方式来对接后端的队列管理服务。

内容的提问来源于stack exchange,提问作者Julian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:45:31