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 badges2 Answers
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
版权声明:本文标题:javascript - Firebase - Count online users - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738087615a2061496.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论