尝试与Twillio Room断开连接时出现未处理的拒绝(错误)

时间:2019-07-24 19:59:12

标签: javascript reactjs twilio twilio-api

在我的twillio应用程序中,尝试断开与房间的连接时出现随机错误。

这是我要在退出时执行的代码:

        this.log('Leaving room...');
        try {
            this.activeRoom ? this.activeRoom.disconnect().catch(console.log) : console.log('No active room to disconnect / check disconnect logic');
        } catch (error) {
            console.log(error)
        }

这会产生以下错误:

Error

我不是很在意错误本身,而是我无法抓住错误。 如您所见,同时添加了try / catch块和.catch(),但仍然出现此错误。

UPDATE_1

在通过api进行挖掘之后,我发现并实现了这一点:

this.activeRoom.on('disconnected', function(room, error) {
  if (error) {
    console.log('Unexpectedly disconnected:', error);
  }
  myRoom.localParticipant.tracks.forEach(function(track) {
    track.stop();
    track.detach();
  });
});

但是这个错误仍然对我有用。

UPDATE_2

实现答案中提出的解决方案后,这就是我得到的(仍然是相同的错误): enter image description here

3 个答案:

答案 0 :(得分:3)

针对遇到此问题的任何人。您从room.disconnect()获得的堆栈跟踪具有误导性。

实际错误出在您的room.localParticipant.publishTrack(track)通话中。该函数返回一个Promise,并会在您调用room.disconnect()的那一刻解决(例外)。

如果您还没有处理过故障,则会收到此问题中显示的错误。因此,只需将.catch()语句添加到所有publishTrack调用中即可。

答案 1 :(得分:2)

这里是Twilio开发人员的传播者。

这很奇怪,您似乎无法捕获该错误。我不确定对此是否有答案。

但是,您可以在尝试断开本地参与者的连接之前先检查其是否已连接。可以“连接”,“断开连接”或“失败”的Participant object has a state property

因此,您可以:

if (this.activeRoom && this.activeRoom.localParticipant.state === 'connected') {
  this.activeRoom.disconnect();
}

答案 2 :(得分:0)

我用它来解决这个问题

(new Promise(() => room.disconnect())).then(() => {}).catch(() => {})