【问题标题】:How to store the chat messages in to ionic storage?如何将聊天消息存储到离子存储中?
【发布时间】:2019-03-02 00:34:03
【问题描述】:
我使用 ionic 和 socket.io-client 创建了一个聊天应用程序。
在我的应用程序中,当我选择一个用户开始聊天时,我的应用程序会从服务器加载最近的聊天数据,然后将其呈现给用户。如果没有网络连接,则用户无法看到之前的聊天消息。所以,我想将聊天消息存储到 ionic 本地存储中,或者有其他更好的方法来存储它们。
请给我一个更好的方法来加载我最近在两个用户之间的聊天,而不是每次选择用户时都从服务器获取。
【问题讨论】:
标签:
ionic-framework
socket.io
local-storage
ionic-storage
【解决方案1】:
你已经说过了,离子存储是要走的路: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) ....
始终只显示本地值并在收到任何内容时更新这些值。您还可以在发送消息时记住对象引用,并在发生任何网络异常时将其标记为不发送。