首页 > 解决方案 > 使用 React Native 进行 Twilio 可编程聊天

问题描述

我正在尝试将 Twilio Chat 添加到我的反应原生项目中。我收到名称SyncError和代码 0 的错误。我只是想确认它此时已连接。这是我的基本设置。

顶部导入

import {Client as Chat} from 'twilio-chat'

在我的班级里面

componentDidMount = async () => {
    console.log(Chat);
    const token = await AsyncStorage.getItem('auth-token');
    axios.get(config.apiUrl + '/chat/details', { headers: { Authorization: token } })
      .then(res => {
        console.log(res);
        Chat.create(res.data.twilioToken)
          .then(client => {
            console.log('client', client);
            this.subscribeToAllChatClientEvents(client);
          })
          .catch(error => {
            console.log('There was an error', error);
          });
      })
      .catch(err => {
        console.log(err);
      })
  }

该错误还提到了“未处理的承诺拒绝”,但我catch在需要的地方包含了任何块。

谢谢你的帮助。

标签: react-nativetwiliotwilio-programmable-chat

解决方案


令牌可能有问题。

根据我的经验,我从后端服务器获得了一个很好的令牌,并将其保存在 AsyncStorage 上。但是,过了一会儿,令牌不再起作用了。

每次我需要实例化 SDK 客户端时,我通过向服务器请求一个新的 twilio 令牌来解决它。


推荐阅读