Firebase - 统计在线用户 [英] Firebase - Count online users

查看:170
本文介绍了Firebase - 统计在线用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 presence ,使其显示一个元素中的总连接用户数。我真的不知道在哪里看。我做了类似的事情:

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

试试看能否找到有用的东西,但是(我有点期待)不能有任何意义的数据。



有什么方法可以帮助我追求什么?获取连接用户的总数,例如。在控制台或元素回显出来?或者,也许如何获取授权用户列表和非授权?编辑:我想保持我的英文拼写,我不是美国人。谢谢。

解决方案

.info / connected 无论当前客户是否连接。为了保持在线计数,您需要通过存储每个用户的在线信息并使用 setOnDisconnect()来创建一个计数器。例如:
$ b

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

//在线时添加到状态列表。
var presenceRef =新的Firebase(https://< url> .firebaseio.com / .info / connected);
presenceRef.on(value,function(snap){
if(snap.val()){
//断开连接时自己删除
userRef.onDisconnect ).remove();

userRef.set(true);
}
});

//在线用户数是在线列表中的对象数。
listRef.on(value,function(snap){
console.log(#online users =+ snap.numChildren());
});


I tried playing about with presence to make it display the total connected users in an element. I couldn't really figure out where to look. I did something similar to this:

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

To try and see if I could find anything useful in there, but (I kinda expected it) I couldn't make any sense of the data.

Is there any way to accomplice what I am after? Fetch the total number of connected users and eg. echo it out in the console or to an element? Or maybe how to fetch a list of authorised users and non-authorised?

EDIT: I would like to keep my spelling in English, I am not American. Thank you.

解决方案

.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());
});    

这篇关于Firebase - 统计在线用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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