抖音开放平台Logo
开发者文档
“/”唤起搜索
控制台
  • JS API 列表
  • 基础
  • TTML
  • 网络
  • 网络 JS API 调用说明
  • 小程序 Cookie 机制
  • HTTP
  • WebSocket
  • EventSource
  • tt.createEventSource
  • EventSourceTask
  • 媒体
  • 地图
  • 文件
  • 数据缓存
  • 地理位置
  • 设备
  • 画布
  • 界面
  • 页面导航
  • 开放接口
  • 行业开放
  • 第三方平台
  • 其它
  • 生活服务(即将废弃)
  • tt.createEventSource

    收藏
    我的收藏

    基础库 1.0.0 开始支持本方法,抖音客户端版本需 >= 30.8,这是一个同步方法。

    小程序流式传输能力,调用后返回一个 EventSource 实例。实例对服务器开启一个持久化的连接,接收 text/event-stream 格式的事件,此连接会一直保持开启直到通过调用实例的 close() 方法关闭。

    前提条件无
    业务背景

    适用于 AI 对话场景流式文本传输。相比于 WebSoceket,EventSource 的实现更轻量。

    使用限制
    • IDE 版本 >= 4.3.5
    注意事项
    • 低版本基础库、抖音未支持该能力,请做好版本兼容。需同时满足:
      • 基础库版本 >= 3.35.0
      • 抖音版本 >= 30.8.0
    支持沙盒
    否
    相关教程无

    语法

    tt.createEventSource(params, timeout)

    参数说明

    params

    类型默认值必填说明最低支持版本
    object是
    接口入参
    3.35.0

    timeout

    类型默认值必填说明最低支持版本
    number60000否

    超时时间(单位:ms)

    4.7.0

    params 类型说明

    object 类型,属性如下:

    属性名类型默认值必填说明最低支持版本
    headerobject否

    HTTP 请求的 header,详情见下方 header 说明

    3.35.0
    methodstring'GET'否

    网络请求方法,支持 'GET' 和 'POST'

    3.35.0
    dataobject | arraybuffer | array无否

    请求的参数,仅 method 为 'POST' 时支持该参数。

    3.35.0
    urlstring无是
    请求地址
    3.35.0

    header 参数说明

    • content-type 默认为 application/json
    • accept 默认为 text/event-stream

    返回值

    类型说明最低支持版本
    object
    EventSourceTask
    3.35.0

    EventSourceTask 参数说明

    返回一个 EventSource 实例。

    扫码体验

    请使用字节宿主APP扫码

    代码示例

    let eventSourceTask; Page({ data: { eventSourceStatus: '', text: '' }, createEventSource() { const eventSourceStatus = this.data.eventSourceStatus; if (eventSourceTask && eventSourceStatus === 'open') return; if (eventSourceTask && eventSourceStatus && eventSourceStatus !== 'open') eventSourceTask.close(); const url = "https://sse-fake.andros.dev/events/"; eventSourceTask = tt.createEventSource({ url }) eventSourceTask.onOpen(() => { console.log('已连接'); this.setData({ eventSourceStatus: 'open' }) }); eventSourceTask.onClose(() => { console.log('eventSource 已断开'); this.setData({ eventSourceStatus: 'close' }) }); eventSourceTask.onError((error) => { console.error('eventSource 发生错误:', error); this.msg('发生错误'); this.setData({ eventSourceStatus: 'error' }); }); eventSourceTask.onMessage((res) => { console.log('eventSource res:', res); const { data, event } = res; if (data && event) { this.setData({ text: this.data.text + `event: ${event}` + '\n' + `data: ${data}` + '\n\n' }); } }); }, closeEventSource() { eventSourceTask.close({ success: res => { console.log('close eventSource success', res); tt.showToast({ title: 'close success', content: JSON.stringify(res), }); this.setData({ eventSourceStatus: 'close' }) }, fail: err => { console.log('close eventSource err', err); } }); }, // 清空数据 clearData() { this.setData({ text: '' }); }, msg(msg) { tt.showToast({ title: msg, icon: 'none' }); }, })
    <view class="container"> <view class="body"> <view class="page-section"> <view class="ttui-cells"> <view class="ttui-cell"> <view class="ttui-cell__bd">EventSource状态:</view> <view class="ttui-cell__ft"> <text>{{eventSourceStatus}}</text> </view> </view> </view> </view> <view class="btn-area"> <button type="primary" size="40" bindtap="createEventSource" disabled="{{eventSourceStatus === 'open'}}">发起请求</button> <button type="primary" size="40" bindtap="closeEventSource" disabled="{{eventSourceStatus !== 'open'}}">关闭连接</button> <button type="primary" size="40" bindtap="clearData">清除数据</button> </view> <text> 收到的数据:</text> <view class="text-box" scroll-y="true" scroll-top="{{scrollTop}}"> <text>{{text}}</text> </view> </view> </view>
    .text-box { margin-bottom: 70rpx; padding: 40rpx; display: flex; min-height: 300rpx; background-color: #FFFFFF; font-size: 30rpx; color: #353535; }