2012-07-31 71 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