首页 > 解决方案 > 如何检查我的用户是否已连接?

问题描述

我做了一个反应钩子来返回这个库的客户端,在这个钩子中,我还连接了用户。

useEffect((): any => {
  async function getSub() {
    const {
      data: { createSupportChatSubscription },
    } = await getSubscription();
    return createSupportChatSubscription.subscription;
  }

  getSub().then(async (res: ISubscription) => {
    const chatClient = StreamChat.getInstance(process.env.REACT_APP_STREAM_ID as string);

    await chatClient.connectUser(
      { id: currentUser.id, name: currentUser.name },
      res.token.token
    );
    setChannelResponse({
      channelId: res.channelId,
      channelType: res.channelType,
    });
    setClient(chatClient);
  });

  return () => client?.disconnectUser();
}, [getSubscription, client, currentUser.id, currentUser.name, userId]);

使用这个钩子时,我倾向于得到这个警告:

Consecutive calls to connectUser is detected, ideally you should only call this function once in your app.

我正在尝试查看聊天客户端是否有任何东西可以检查是否有连接,所以我不必经常打电话connectUser

标签: javascriptgetstream-io

解决方案


推荐阅读