2012-07-31 73 views
1

使用Meteor框架记录连接数的最佳方法是什么?我有要求在线共享用户,并采取了创建一个集合,只是为每个用户更换初始化记录,但计数似乎重置,我到目前为止在下面,由于先进。流星连接数

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 

回答

1

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

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

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