【问题标题】:Firebase how to know specific client is offline?Firebase 如何知道特定客户端离线?
【发布时间】:2018-01-17 14:22:08
【问题描述】:

我正在使用树莓派和 Firebase 开发设备/产品...

系统流程如下:用户在我的网站上购买设备,她可以使用相同的凭据登录移动应用程序。

目前,我依靠一些如何生成的 uniuqe id 放入设备中,以便从设备和设备到 Firebase 实时数据库与 db 进行交互......

现在我需要知道设备是在线还是离线。答案应该是可能的两个解决方案。因为我想通知用户:“您的设备离线”...

1- 基于当前代码(依赖唯一 ID):如果可能,我该如何实现。

2- 将系统更改为基于用户的系统:如果我实现了这个,我必须如何思考系统。设备将没有显示输入或键盘。例如当用户更改她的密码时会发生什么?

谢谢你..

【问题讨论】:

标签: firebase firebase-authentication firebase-cloud-messaging


【解决方案1】:

你想要的是onDisconnect

当您的设备启动时,您设置一个onDisconnect 以更改其状态甚至触发时间:

var lastOnlineRef = firebase.database().ref("devices/1234/lastOnline");
lastOnlineRef.onDisconnect().set(firebase.database.ServerValue.TIMESTAMP);

var isOnlineRef = firebase.database().ref("devices/1234/isOnline");
isOnlineRef.onDisconnect().set(false);

至于用户的事情,我不确定我是否理解。如果您将设备与您的 UID 隔离在如下路径中:/devices/UID,它们独立于用户而存在。

对于您的用户,您只需创建他们所有设备的列表:/users/UID/devices

{
    1234: 1234,
    5678: 5678
}

然后在您的应用中,您只需首先获取他们的设备列表,然后创建对它们的引用。

在 Angular 中看起来像:

// in the ts
public userDevices = this.db.list('/users/UID/devices').valueChanges();

public getDeviceObservable(deviceId: string) {
    return this.db.object('/devices/' + deviceId).valueChanges();
}

//in the template
<div *ngFor="let deviceId of userDevices | async">
    <ng-container *ngIf="getDeviceObservable(deviceId) | async as device">
    {{ device | json }}
    </ng-container>
</div>

在模板中放置 getObservable 并不是最佳实践,您也可以在组件中这样做,但与 switchMap 等完全是另一回事。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 2018-11-07
    • 2012-03-27
    • 1970-01-01
    相关资源
    最近更新 更多