Twilio 可编程聊天 getChannelByUniqueName 抛出“禁止”错误

问题描述

在我的后端服务中创建一个频道后,我试图在我的 React 前端中加入一个像这样的 useEffect 钩子的频道:

const initchat = async () => {

    const chatClient = await Client.create(props.token);
    const channel = await chatClient.getChannelByUniqueName(props.room);

    if (channel) {
      if (channel.status !== "joined") {
        chatClient.on("channelJoined",() => {
          registerChannelListeners(channel);
        });

        await channel.join();
      } else {
        registerChannelListeners(channel);
      }

      setChannel(channel);
      setMessages((await channel?.getMessages()).items);
    }
  };
}

最初导航到页面时,错误

upstream.js?a850:136 Uncaught (in promise) Error: Forbidden
    at Upstream.actualSend (upstream.js?a850:136)

偶尔被抛出。

重新加载页面后,一切正常。

问题线似乎是:

const channel = await chatClient.getChannelByUniqueName(props.room);

因为没有进一步的代码被执行。 tokenroom 都分配有有效值。

在解码的套接字消息中,这个错误消息是从 twilio 发送的:

{"method":"reply"...,"http_status":{"code":403,"status":"Forbidden"}}
{"status":403,"message":"User not member of channel","code":50400}

虽然两个参与者都是通过后端通过此功能邀请的:

inviteParticipanttochannel(participant: Participant,channelSid: string) {
    this.logger.log(
      `Inviting paricipant ${participant.getIdentifier()} to channel ${channelSid}`,);

    return this.twilioClient.chat
      .services(process.env.TWILIO_CHAT_SERVICE_ID)
      .channels(channelSid)
      .invites.create({ identity: participant.getIdentifier() });
  }

我还需要做些什么才能让参与者找到/加入频道?

解决方法

您是否尝试过设置一个短暂的超时时间? twilio 的频道资源可能需要一些时间。