admin管理员组

文章数量:1178545

I tried using presence to make it display the total connected users in an element. I can't really figure out how to make it work.

I also tried doing the following:

    var dataUlist = new Firebase('https://<url>.firebaseio/.info/connected');
    dataUlist.on('value', function(snap) {
        console.log(snap);
    });

To tried to see if I could find anything useful in there, but I couldn't figure out how the data works.

Is there any way to accomplice what I am after? Fetch the total number of connected users and then echo it out in the console or to an element?

I tried using presence to make it display the total connected users in an element. I can't really figure out how to make it work.

I also tried doing the following:

    var dataUlist = new Firebase('https://<url>.firebaseio.com/.info/connected');
    dataUlist.on('value', function(snap) {
        console.log(snap);
    });

To tried to see if I could find anything useful in there, but I couldn't figure out how the data works.

Is there any way to accomplice what I am after? Fetch the total number of connected users and then echo it out in the console or to an element?

Share Improve this question edited Apr 10, 2022 at 21:43 Bella 1121 silver badge17 bronze badges asked Apr 12, 2013 at 23:04 MrEMrE 1,1543 gold badges14 silver badges28 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 34

.info/connected will only return information about whether the current client is connected or not. In order to maintain a presence count, you'll need to create a counter by storing presence information for each user and utilizing setOnDisconnect(). For example:

var listRef = new Firebase("https://<url>.firebaseio.com/presence/");
var userRef = listRef.push();

// Add ourselves to presence list when online.
var presenceRef = new Firebase("https://<url>.firebaseio.com/.info/connected");
presenceRef.on("value", function(snap) {
  if (snap.val()) {
    // Remove ourselves when we disconnect.
    userRef.onDisconnect().remove();

    userRef.set(true);
  }
});

// Number of online users is the number of objects in the presence list.
listRef.on("value", function(snap) {
  console.log("# of online users = " + snap.numChildren());
});    

Here is the the code from Anant formatted for Android

public void getDbCount() {

    Firebase listRef = new Firebase("https://<your-firebase-database>.firebaseio.com/presence/");
    final Firebase userRef = listRef.push();

    // Add ourselves to presence list when online.
    Firebase presenceRef = new Firebase("https://<your-firebase-database>.firebaseio.com/.info/connected");

    ValueEventListener myPresence = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            // Remove ourselves when we disconnect.
            userRef.onDisconnect().removeValue();
            userRef.setValue(true);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
            Log.e("DBCount", "The read failed: " + firebaseError.getMessage());
        }
    };

    presenceRef.addValueEventListener(myPresence);

    // Number of online users is the number of objects in the presence list.
    ValueEventListener myList = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            // Remove ourselves when we disconnect.
            Log.i("DBCount", "# of online users = " + String.valueOf(snapshot.getChildrenCount()));
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
            Log.e("DBCount", "The read failed: " + firebaseError.getMessage());
        }
    };

    listRef.addValueEventListener(myList);
}

本文标签: javascriptFirebaseCount online usersStack Overflow