将数组从服务器发布到客户端

时间:2014-01-13 06:13:55

标签: javascript meteor

如果Meteor.isServer在登录时将userIds推送到数组并在注销时删除了所述用户ID,我就有了一个数组。为了做到这一点,我使用的是Profile-Online软件包(https://github.com/erundook/meteor-profile-online)。我想制作独特的模板,具体取决于连接0,1或2个用户的用户数量我需要使服务器端的阵列可供客户端使用而不用它我不能根据用户数量制作模板。我试图在服务器中发布数组并在客户端订阅。我有一个想法,我正在做一些完全错误的事情。我的主要目标是使数组(playerArray)可以从客户端访问。

JS档案:

Games = new Meteor.Collection("games")
if (Meteor.isClient) {
    //testing if subscribe returns IDs from the array which it doesn't
    Meteor.subscribe("ingame",function(test){console.log(test)})
}
if (Meteor.isServer) {
  playerArray = []
  Meteor._onLogin  = function (userId){
    if(!_.contains(playerArray, userId)){
      playerArray.push(userId)
    }
    console.log(playerArray)
  }
   Meteor._onLogout  = function (userId){
    playerArray = _.without(playerArray, userId);
    console.log(playerArray);
   }
   Meteor.publish("ingame",function(){
    return playerArray
   })
} 

1 个答案:

答案 0 :(得分:0)

我是流星的新手,但这应该有效:

  1. 创建一个集CurrentUsers,您可以在_onLogin上插入该集合_onLogout
  2. 然后在客户端询问ClientUsers.find({}).count()并返回登录用户数
  3. 我怀疑阵列方法不可扩展(不适用于多个节点进程),但这应该可以正常工作