【问题标题】:How to know how many people are connected to Firebase in Android studio?如何知道有多少人连接到 Android Studio 中的 Firebase?
【发布时间】:2017-04-11 12:14:27
【问题描述】:

很简单的问题。我正在制作一个 android 应用程序,它使用 firebase 身份验证让用户通过电子邮件和密码连接,以及 firebase 实时数据库服务。

我想知道我是否可以通过 android studio 检查当前有多少人连接到我的数据库(通过 Firebase 身份验证)

提前致谢!

【问题讨论】:

标签: android firebase firebase-realtime-database firebase-authentication


【解决方案1】:
var database = firebase.database();

//   connecting users
var connectionsRef = database.ref("/connections");

var connectedRef = database.ref(".info/connected");
// Number of online users is the number of objects in the presence list.

// When the client's connection state changes...
connectedRef.on("value", function(snap) {

    // If they are connected..
    if (snap.val()) {

        // Add user to the connections list.
        var con = connectionsRef.push(true);

        // Remove user from the connection list when they disconnect.
       con.onDisconnect().remove();
    }
});

// When first loaded or when the connections list changes...
connectionsRef.on("value", function(snapshot) {

    // Display the viewer count in the html.
    // The number of online users is the number of children in the connections list.
    console.log("numers of players are:"+ snapshot.numChildren());
    $(".displayDiv").text(snapshot.numChildren());

【讨论】:

    【解决方案2】:

    这是一个老问题,但如果有人正在寻找解决方案,我在 Firebase 文档上找到了一些信息: https://firebase.google.com/docs/firestore/solutions/presence

    【讨论】:

      【解决方案3】:

      这很简单。每次用户连接到您的应用时,将他添加到名为 users 的 Firebase 类别中。然后您可以使用以下代码行查询该类别以查看确切的用户数:

      int numberOfUsers = dataSnapshot.getChildrenCount();
      

      【讨论】:

      • 我基本上想知道现在有多少人在玩这个游戏,所有玩的玩家都连接到firebase。那么我怎么知道什么时候删除它们?(如果整个应用程序关闭并且它们停止播放,我只需要删除它们)..
      • 有两种不同的东西。一是有多少玩家连接到 Firebase,二是有多少玩家连接到 Firebase 并同时在玩。所以,为了回答第二个问题,因为第一个你有上面的答案,你需要在你的数据库中创建另一个名为onlineStatus的文件,如果玩家是@987654325,你必须在其中输入true @ 和 false 如果不是。您还需要在该字段上添加ValueEventListener 以实时查看播放状态并采取相应措施。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-28
      • 1970-01-01
      • 1970-01-01
      • 2019-05-12
      • 2017-03-24
      • 2020-06-24
      相关资源
      最近更新 更多