Ionic3 - 如何与存储提供者实现持久聊天系统?
Ionic3 - How to implement a persistent chat system with Storage provider?
我想用 Ionic3
创建一个基本的聊天系统。
我希望消息保留在应用程序中,所以我想我需要使用 Sotrage
提供程序,类似于他们在官方网站上推荐的内容:
import { Storage } from '@ionic/storage';
export class MyApp {
constructor(private storage: Storage) { }
...
// set a key/value
storage.set('name', 'Max');
// Or to get a key/value pair
storage.get('age').then((val) => {
console.log('Your age is', val);
});
}
但我的问题是,您认为保持消息顺序以便应用程序正确加载消息的最佳方式是什么?
也许会做类似...
保存消息时:
// this is just a quick idea. please, improve it if possible
let message = {
channel: <channel>,
timestamp: <timestamp>,
from: <from>,
to: <to>,
content: <content>,
}
storage.set('message', {});
检索完整频道消息时:
??? how to apply filters to retrieve messages from an specific channel in the proper order ???
我的疑问是因为我没有看到存储提供程序有特殊的方法来过滤数据和做一些奇特的事情。所以我正在寻找这方面的建议。
谢谢!
对于 'fancy things' 你可以使用 LokiJS。 LokiJS 是一个内存数据库,有可以链接的集合 find/sort/filter/map 等...完整的数据库可以作为 json 对象保存到离子存储中。
网站http://lokijs.org目前处于离线状态。
文档在线 here
您可以使用本机存储,只需记得将新消息加入旧对象并再次保存即可。
然后在 html 中用 ngFor 和管道迭代对象 *ngFor="let message of messages | orderBy : ['timestamp']"
执行此操作的其他方法是使用 ionic 的 sqlite 包。就像普通数据库一样 https://ionicframework.com/docs/native/sqlite/
使用可观察对象。
Localstorage returns promises,所以你可以使用 Observable.fromPromise(this.localStorage.getMessages()) 将返回的 promise 变成 Observable
然后*ngFor="let message of messages | async "
可以添加一个定时订阅者以定期检查 localStorage 的更新。
您想自己建立聊天有什么特别的原因吗?
你可以看看对讲机,它有一个 cordova 和 ionic 包。
我想用 Ionic3
创建一个基本的聊天系统。
我希望消息保留在应用程序中,所以我想我需要使用 Sotrage
提供程序,类似于他们在官方网站上推荐的内容:
import { Storage } from '@ionic/storage';
export class MyApp {
constructor(private storage: Storage) { }
...
// set a key/value
storage.set('name', 'Max');
// Or to get a key/value pair
storage.get('age').then((val) => {
console.log('Your age is', val);
});
}
但我的问题是,您认为保持消息顺序以便应用程序正确加载消息的最佳方式是什么?
也许会做类似...
保存消息时:
// this is just a quick idea. please, improve it if possible
let message = {
channel: <channel>,
timestamp: <timestamp>,
from: <from>,
to: <to>,
content: <content>,
}
storage.set('message', {});
检索完整频道消息时:
??? how to apply filters to retrieve messages from an specific channel in the proper order ???
我的疑问是因为我没有看到存储提供程序有特殊的方法来过滤数据和做一些奇特的事情。所以我正在寻找这方面的建议。
谢谢!
对于 'fancy things' 你可以使用 LokiJS。 LokiJS 是一个内存数据库,有可以链接的集合 find/sort/filter/map 等...完整的数据库可以作为 json 对象保存到离子存储中。
网站http://lokijs.org目前处于离线状态。 文档在线 here
您可以使用本机存储,只需记得将新消息加入旧对象并再次保存即可。
然后在 html 中用 ngFor 和管道迭代对象 *ngFor="let message of messages | orderBy : ['timestamp']"
执行此操作的其他方法是使用 ionic 的 sqlite 包。就像普通数据库一样 https://ionicframework.com/docs/native/sqlite/
使用可观察对象。
Localstorage returns promises,所以你可以使用 Observable.fromPromise(this.localStorage.getMessages()) 将返回的 promise 变成 Observable
然后*ngFor="let message of messages | async "
可以添加一个定时订阅者以定期检查 localStorage 的更新。
您想自己建立聊天有什么特别的原因吗?
你可以看看对讲机,它有一个 cordova 和 ionic 包。