meteor连线数

使用Meteor框架来logging连接数的最好方法是什么? 我有在线共享用户的要求,并已经采取了创build一个集合,只是为每个用户更换初始化logging,但计数似乎重置,我已经到目前为止,在先进的感谢。

Counts = new Meteor.Collection "counts" if Meteor.is_client if Counts.findOne() new_count = Counts.findOne().count + 1 Counts.remove {} Counts.insert count: new_count Template.visitors.count = -> Counts.findOne().count if Meteor.is_server reset_data = -> Counts.remove {} Counts.insert count: 0 Meteor.startup -> reset_data() if Counts.find().count() is 0 

当你信任“获得计数值,从集合中删除,插入集合新计数”时,你会有一个竞争条件。 客户可以在同一时间获得价值X. 这不是要走的路。

相反,尝试使每个客户端插入一个集合中的“自己”。 把一个唯一的ID和它插入的“时间”。 使用Meteor.Method实现心跳,刷新这个“时间”。 时间过久的客户可以从集合中删除。 使用服务器中的定时器来删除空闲的客户端。

你可以在这里检查一下: https : //github.com/francisbyrne/hangwithme/blob/master/server/game.js