繁体   English   中英

在多频道聊天应用程序中,如何显示仅与特定聊天频道有关的消息?

[英]In a multichannel chat application how can I display messages pertaining only to a specific chat channel?

环境:expo 36.0.0 / React Native 0.61 / react-native-gifted-chat 0.13.0 / firebase 7.6.0

我的目标:仅在天才聊天 UI 上呈现特定于特定聊天频道的消息。

预期结果:天才聊天 UI 上应仅显示与给定频道有关的消息。

实际结果 :

  1. 当我从一个聊天频道导航到另一个聊天频道时,总消息会累积。
  2. 当我回到较早的频道时,同一聊天频道的消息重复不止一次。
  3. 警告:遇到两个具有相同密钥%s孩子。 键应该是唯一的,以便组件在更新时保持其身份。 非唯一键可能会导致子项重复和/或省略 — 该行为不受支持并且可能在未来版本中更改。%s

到目前为止我尝试了什么? 1. 每次用户使用 componentDidUpdate 从一个频道导航到另一个频道时,重新启动对新聊天频道的订阅。 2. 每次用户更改聊天频道时,将消息数组的状态设置为空数组。 3. 在 Firebase 中取消订阅上一个节点并在 componentDidUpdate 中订阅一个新节点。 这里的节点代表 Firebase 中由 ID 标识的聊天频道。 每个节点都包含与该特定聊天频道有关的所有消息的子节点。

  async componentDidMount() {
    await this.getSessionInfo();
    await this.getCurrentProfile();
    await this.getMessages();
  };

  async componentDidUpdate(prevProps) {
    /* sessionID represent a chat channel and corresponding node in Firebase */
    /* If user navigates from one channel to another we establish a connection with new node and get the 
    corresponding messages */
    if (this.props.navigation.state.params.sessionID !== prevProps.navigation.state.params.sessionID) {
      await this.getSessionInfo();
      await this.getCurrentProfile();
      /* Subscribe to new chat channel */
      await this.ref();
      await this.getMessages();
    }
  };

  async componentWillUnmount() {
    /* Unsubscribe from database */
    await this.off();
  }

  /* Get messages to display after component is mounted on screen */
  getMessages = () => {
    this.connect(message => {
      this.setState(previousState => ({
        messages: GiftedChat.append(previousState.messages, message),
      }))
    });
  }

  /* Each sessionID corresponds to a chat channel and node in Firebase  */
  ref = () => {
    return database.ref(`/sessions/${this.state.sessionID}`);
  }

  /* Fetch last 20 messages pertaining to given chat channel and lister to parse new incoming message */
  connect = (callback) => {
    this.ref()
      .limitToLast(20)
      .on('child_added', snapshot => callback(this.parse(snapshot)));
  }

    /* newly created message object in GiftedChat format */
    parse = snapshot => {
    const { timestamp: numberStamp, text, user } = snapshot.val();
    const { key: _id } = snapshot;
    const timestamp = new Date(numberStamp);

    const message = {
      _id,
      timestamp,
      text,
      user,
    };
    return message;
  };

  /* function that accepts an array of messages then loop through messages */
  send = (messages) => {
    for (let i = 0; i < messages.length; i++) {
      const { text, user } = messages[i];
      const message = {
        text,
        user
      };
      this.append(message);
    }
  };

  /* append function will save the message object with a unique ID */
  append = message => this.ref().push(message);

按照 Brett Gregson 的建议和 Mike Kamerman 的回答,我实施了以下解决方案。

  1. 当用户从一个频道导航到另一个频道时,我会触发一组功能。 this.ref() 函数订阅新频道(Firebase 中的节点)。
  async componentDidUpdate(prevProps) {
    if (this.props.navigation.state.params.sessionID !== prevProps.navigation.state.params.sessionID) {
      await this.getSessionInfo();
      await this.ref();
      await this.switchSession();
    }
  };
  1. 我将消息状态(消息数组)设置为空数组以及异步/等待机制。 getMessages 函数获取新通道的消息。 这样,属于前一组的消息将被清除,并且 UI 中没有消息堆积。
  switchSession = async () => {
    await this.setState({ messages: [] });
    await this.getMessages();
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM