如何知道在Android工作室中有多少人连接到Firebase?

时间:2017-04-11 12:14:28

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

非常直截了当的问题。我正在制作一个使用firebase身份验证的Android应用程序,用户可以使用电子邮件和密码以及firebase实时数据库服务进行连接。

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

提前感谢!

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

这非常简单。每次用户连接到您的应用时,请将其添加到名为users的Firebase类别中。您可以查询该类别,以查看具体的用户数:int numberOfUsers = dataSnapshot.getChildrenCount();。希望它有所帮助。

答案 2 :(得分:0)

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());
相关问题