【问题标题】:Twilio Programmable Chat getChannelByUniqueName throws "Forbidden" ErrorTwilio 可编程聊天 getChannelByUniqueName 引发“禁止”错误
【发布时间】:2021-05-07 15:24:07
【问题描述】:

在我的后端服务中创建一个频道后,我试图在我的反应前端中加入这个频道,像这样的 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() });
  }

我需要做更多的事情来让参与者找到/加入频道吗?

【问题讨论】:

    标签: node.js reactjs twilio use-effect twilio-programmable-chat


    【解决方案1】:

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

    【讨论】:

    • 设置超时确实有效,谢谢。这仍然有点奇怪,因为我在后端代码中明确等待 inviteParticipantToChannel() 调用。但看起来invites.create() 只是触发了创建邀请的过程并提前返回。
    猜你喜欢
    • 2017-07-01
    • 2019-09-22
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    相关资源
    最近更新 更多