如何将聊天消息存储到离子存储器中?

How to store the chat messages in to ionic storage?

我使用 ionic 和 socket.io-client 创建了一个聊天应用程序。

在我的应用程序中,当我 select 一个用户开始聊天时,我的应用程序从服务器加载最近的聊天数据,然后将其呈现给用户。如果没有网络连接,则用户无法看到之前的聊天消息。所以,我想将聊天消息存储到离子本地存储中,或者有任何其他更好的存储方式。

请告诉我一个更好的方法来加载我最近在两个用户之间的聊天,而不是每次用户 selected 时都从服务器获取。

您已经说过了,离子存储是必经之路:https://ionicframework.com/docs/storage/

对于每次对话我都会:

1. Send message to server 
2. storage.get(conversationID) ....
3. add message and last message send timestamp to conversation
4. storage.set(conversationID) ....

加载消息时:

1. storage.get(conversationID) ....
2. Check if newer messages are available
3. If yes -> fetch 
3.1. add messages and last message send timestamp to conversation
4. storage.set(conversationID) ....

始终只显示本地值并在收到任何内容时更新这些值。您还可以在发送消息时记住对象引用,并在发生任何网络异常时将其标记为不发送。