如何在 Meteor 中跟踪服务器端匿名用户的数量? [英] How to track the number of anonymous users server-side in Meteor?

查看:55
本文介绍了如何在 Meteor 中跟踪服务器端匿名用户的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Meteor 编写一个数据敏感的应用程序,并试图限制客户端访问尽可能多的信息.因此,我想在服务器端实现一种计算登录用户和匿名用户数量的方法.

I'm writing a data-sensitive application in Meteor, and am trying to limit the client access to as much information as possible. Therefore, I want to implement server side a way of counting the number of logged-in, and anonymous, users.

我尝试了多种方法.第一个是在这个问题中概述的客户端断开连接后的服务器清理,其中建议挂钩:

I have tried a variety of methods. The first was as outlined in this question Server cleanup after a client disconnects, which suggests hooking into:

this.session.socket.on("close")

但是,当我这样做并尝试更改集合时,它抛出了Meteor 代码必须始终在 Fiber 内运行"错误.我认为这个问题是因为一旦套接字关闭,Fiber 就会被杀死,因此无法访问数据库.OP 指出了这个 Meteor 代码必须始终在 Fiber 中运行"在服务器上调用 Collection.insert 时 作为一种可能的解决方案,但根据对答案的评论,我不确定这是否是最好的方法.

However when I did, and tried to change a collection, it threw a "Meteor code must always run within a Fiber" error. I assume this problem is because once the socket is closed, that Fiber is killed, and so accessing the database is impossible. The OP pointed to this "Meteor code must always run within a Fiber" when calling Collection.insert on server as a possible solution, but I wasn't sure if that's the best method, based on the comments to the answer.

然后我尝试在变量上自动运行:

I then tried to autorun on the variable:

Meteor.default_server.stream_server.all_sockets().length

但似乎从未调用过自动运行,所以我假设该变量不是响应式上下文,而且我不确定如何将其设为一个.

but the autorun never seemed to be called, so I'm assuming that variable is not a reactive context, and I wasn't sure how to make it one.

最后一个想法是做一个 keepalive 风格的事情,但这似乎完全违背了 Meteor 的理念,我想我只会将其用作绝对的最后手段.

The last idea was to do a keepalive style thing, but that seems to completely go against the grain of the Meteor philosophy, and I think I'll only use as an absolute last resort.

我对 this.session.socket 上的函数做了一个 console.log,唯一可能的其他函数是 .on("data"),但是当套接字关闭时不会调用它.

I did a console.log of the functions on this.session.socket, and the only other function possible was .on("data"), but this isn't called when the socket is closed.

我在这里有点不知所措,所以任何帮助都会很棒,谢谢.

I'm at a bit of a loss here, so any help would be great, Thanks.

推荐答案

为了完整起见,最好将上述两个答案结合起来.换句话说,请执行以下操作:

For the sake of completeness, it's probably best to combine the two answers above. In other words, do the following:

  • Keep the online status in Meteor.users, as opposed to a separate collection, as in https://github.com/erundook/meteor-profile-online
  • Keep track of disconnections via callback, instead of a heartbeat, as in https://github.com/murilopolese/howmanypeoplearelooking

这可能是在 Meteor 中实现这一点的规范方式.我已经将它创建为一个可以与 Meteorite 一起安装的智能包:https://github.com/mizzao/meteor-user-status

This would probably be the canonical way to implement this in Meteor. I've created this as a smart package that you can install with Meteorite: https://github.com/mizzao/meteor-user-status

这篇关于如何在 Meteor 中跟踪服务器端匿名用户的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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