如何处理Firebase上同一用户的多个连接? [英] How to handle multiple connections of the same user on Firebase?

查看:210
本文介绍了如何处理Firebase上同一用户的多个连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何管理同一用户的并发连接。有什么办法可以限制我的用户可以保持活动的连接数量吗?

我可以让我的数据库如下所示:

 users:{
...
FAKE-USER-ID:{
active_connections:2
}
...
}

更新<$ c

连接丢失应用程序的情况崩溃将在这种情况下被管理?有没有一个Firebase原生的方式来做到这一点?

解决方案

要处理用户连接,请查看 .info / connected 。你可以用它来设置一个标志,当用户连接。



要处理用户断开连接时(通过关闭应用程序或失去连接),看看 onDisconnect() 。您可以在用户断开连接时删除该标志。



但是,您为什么要限制用户只能在一台设备上访问他们的数据呢? Firebase不会为每个连接收费,那么为什么要限制呢?几年前,我正在使用一个展示了这种确切行为的应用程序,而我发现自己每天多次被锁定在应用程序外面。我转移到另一个应用程序。


How to manage concurrent connections of the same user. Is there any way I can limit how many connections my user can keep active?

I could make my database something like:

"users": {
    ...
    "FAKE-USER-ID": {
        "active_connections": 2
    }
    ...
}

And update the key active_connections whenever a user changes their state.

How cases like connection lost and app crashes would be managed in this scenario? Is there a Firebase native way of doing this?

解决方案

To handle when the user connects, look at .info/connected. You could use this to set a flag when the user connects.

To handle when the user disconnects (either by closing the app or because they lose their connection), look at onDisconnect(). You could this to remove the flag when the user disconnects.

But why would you want to limit the user to only access their data on a single device? Firebase doesn't charge per connection, so why would you want to limit it on that?

A few years ago I was using an app that exhibited this exact behavior and I found myself locked out of the app multiple times per day. I moved over to another app.

这篇关于如何处理Firebase上同一用户的多个连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆