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

Twilio Video Web端如何捕获Ringing振铃事件

实现方法

首先明确:ringing 振铃事件不会在已成功连接的 Room 实例上触发,所以不能直接在你现有的 registerRoomEvents 方法里给 room 对象绑定该事件,需要在发起通话连接的阶段提前监听。

具体修改步骤

  • 调整发起通话连接的代码逻辑,不要直接 await Video.connect() 的返回结果——因为等 await 执行完成拿到 Room 实例时,对方已经接听,振铃阶段早就结束了。
  • 在拿到 Video.connect() 返回的thenable连接对象后,立刻绑定ringing事件监听,在回调里触发页面显示"Ringing..."的逻辑。
  • 等连接成功 resolve 出Room实例后,再执行你原有的registerRoomEvents方法注册房间事件。

代码示例

修改后的发起通话逻辑

// 原有connect配置保持不变,不要直接await
const roomConnection = Video.connect(yourToken, {
  name: '目标房间名',
  audio: true,
  video: true
  // 其余你原来的连接配置
});

// 提前绑定振铃事件
roomConnection.on('ringing', () => {
  // 执行页面提示逻辑,比如把状态文本设置为"Ringing..."
  this.statusText.innerText = 'Ringing...';
});

// 可选:监听连接错误、被叫拒接等场景,关闭振铃提示
roomConnection.on('error', () => {
  this.statusText.innerText = '呼叫失败';
});

// 等连接成功后,注册原有房间事件
const room = await roomConnection;
this.registerRoomEvents(room);

补充原有事件逻辑

你可以在已有的participantConnected事件回调里加关闭振铃提示的逻辑——这个事件触发代表被叫已经成功加入房间,振铃结束:

registerRoomEvents(room: Video.Room) {
  room
    .on('participantConnected', (participant) => {
      // 新增:被叫接通,隐藏Ringing提示
      this.statusText.innerText = '已接通';
      this.addParticipant(participant);
    })
    .on('participantDisconnected', (participant) => {
      this.removeAwaitingParticipant(participant.identity);
    })
    .on('disconnected', this.onRoomDisconnection)
    .on('dominantSpeakerChanged', this.onDominantSpeakerChanged);
}

注意事项

  • ringing事件仅在主叫侧触发,触发时机是服务端确认被叫端开始振铃、尚未接听的节点
  • 不要尝试在registerRoomEvents内给已连接的room实例绑定ringing事件,已完成连接的Room实例不会抛出该事件
  • 如果呼叫被拒、超时、无人接听,会触发连接错误,记得在错误回调里关闭振铃提示,避免页面状态异常

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.02 21:24:30