2014-05-18 10 views

回答

1

首先定义pubnub你publish_keysubscribe_key

var pubnub = PUBNUB.init({ 
    publish_key: 'demo', 
    subscribe_key: 'demo' 
}); 

要获得所有通道占用(住户和占据计数)

pubnub.here_now({ 
    callback : function(m){console.log(JSON.stringify(m))} 
}); 

要返回频道列表,与相关的认购关键,用户存在的地方。

pubnub.where_now({ 
    callback : function(m){console.log(JSON.stringify(m))}, 
    error : function(m){console.log(JSON.stringify(m))} 
}); 

UPDATE

要获得所选通道的占用信息。 (类似的东西到复用)

var pubnub = PUBNUB.init({ 
    publish_key: 'demo', 
    subscribe_key: 'demo' 
}); 

var myChannels = ['AAPL', 'SOFIX']; // define your channels here 
pubnub.here_now({ 
    callback : function(m){ 
     var result = {}; 
     for (var i=0; i < myChannels.length; i++) { 
      if (myChannels[i] in m.channels){ 
       result[myChannels[i]] = m.channels[myChannels[i]]; 
      } 
     } 
     console.log(JSON.stringify(result)); 
    } 
}); 

参考文献:

  1. here_now:link
  2. where_now:link
  3. 列表中的所有pubnub通道与活跃用户:link
+0

我想这并且无法复用以使用它! (当使用多个频道时) – kidcapital

+0

使用不带'频道'的'pubnub.here_now'应返回使用'publish_key'和'subscribe_key'创建的所有频道(及其占用)。 – Grainier

+0

我更新了我的答案,检查是否有帮助。 – Grainier