首页 > 解决方案 > Twilio 聊天 React Native SDK 错误:无法添加命令

问题描述

我正在使用 React Native 中的 SDK 客户端使用 Twilio 可编程聊天将聊天添加到我的应用程序中。发送消息的代码如下:

client.sendMessage(message.text)
    .catch(err => console.log(err));

我在控制台中收到一个错误,上面写着:

Error: Can't add command: (status: 0, code: 0)
    at session.js:173
    at tryCallOne (core.js:37)
    at core.js:123
    at JSTimers.js:294
    at _callTimer (JSTimers.js:151)
    at _callImmediatesPass (JSTimers.js:199)
    at Object.callImmediates (JSTimers.js:463)
    at MessageQueue.__callImmediates (MessageQueue.js:316)
    at MessageQueue.js:136
    at MessageQueue.__guard (MessageQueue.js:291)

我正在抓住它,所以它不会在我的实际应用程序中造成任何问题,但很高兴了解导致它的原因以及如何修复它。

注意:消息正在发送,所有功能看起来都很好。

谢谢你的帮助

标签: react-nativetwiliotwilio-programmable-chat

解决方案


我最终能够摆脱这个问题。这是由于我的 leaveChannel() 方法使用了不正确的 Promise 链。自从解决了这个问题后,我对 add 命令错误没有任何问题,我认为这是由于房间没有正确断开连接造成的。如果有帮助,以下是我的断开连接方法。让我知道你是怎么做出来的。

leaveChannel() {
return new Promise((resolve, reject) => {
  if (this.channel) {
    this.channel.removeAllListeners();
    this.channel
      .leave()
      .then((leftChannel: Channel) => {
        console.log("Left chat channel: " + leftChannel.uniqueName);
        store.dispatch(chatSetState(ConnectionStateEnum.DISCONNECTED));
        resolve();
      })
      .catch((error: any) => {
        console.log("leaveChannel(): ", error);
        this.channel = null;
        reject(error);
      });
  } else {
    console.log("Not currently in a channel.");
  }
});}

推荐阅读