javascript - 授予成功后无法订阅 channel

标签 javascript php publish-subscribe pubnub

这是我在 PHP 中所做的:

$pubnub = new Pubnub(array(
  'subscribe_key' => '<SUBSCRIBE_KEY>',
  'publish_key' => '<PUBLISH_KEY>',
  'secret_key' => '<SECRET_KEY>',
  'uuid' => $uuid,
));
$grants = $pubnub->grant(true, true, $channel, $auth_key, 0);

行得通。我的身份验证 key 和正确的访问得到了 200 响应。

然后我在 JS 中这样做:

var pubnub = PUBNUB.init({
  auth_key: settings.auth_key,
  subscribe_key: settings.subscribe_key,
  publish_key: settings.publish_key,
  uuid: settings.uuid,
});

pubnub.subscribe({
  channel: settings.channel,
  // auth_key: settings.auth_key,
  // uuid: settings.uuid,
  presence: function(m) {
    console.log('presence', m);
  },
  message: function(m) {
    console.log('message', m);
  }
});

并且每秒会产生大约 10 403 个错误。我试过包括和排除一堆配置变量,比如 uuidauth_key ,但我得到的只是很多 403。

如果我包括 origin: 'pubsub.pubnub.com' , presence事件触发一次或两次,但随后仍然是一大堆 403。如果我不包含任何 origin ,只有 403,没有事件。

here_now()在 JS 中工作正常,但是 uuids是空的,occupancy为 0:

setInterval(function() {
  pubnub.here_now({channel: settings.channel}, function(rsp) {
    console.log('here_now', rsp);
  });
}, 2000);

为什么 PHP grant工作,但不是 JS subscribe

最佳答案

PubNub 状态和访问管理器

任何时候您使用 Presence连同 Access Manager (免费包含在所有帐户中)在 PubNub 中,当您授予 channel 权限时,如果客户端要监视状态(实现状态回调),您还需要授予对这些 channel 的 -pnpres 对应项的访问权限或在 channel 上启用状态 - 取决于 SDK 如何处理)。

$grants = $pubnub->grant(true, true, 'sports,sports-pnpres', $auth_key, 0);

此代码示例展示了如何在一次调用中授予多个 channel 。

关于javascript - 授予成功后无法订阅 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33920724/

相关文章:

javascript - Backbone UI 模型与 Handlebars 绑定(bind)

javascript - 如何将鼠标悬停在 Javascript 中的矩形上?

php - 正则表达式:将所有 URL 设为绝对

asynchronous - 使用 ZeroRPC 流式传输

javascript - 我可以使用 Angular2 Universal 仅在客户端中运行组件方法吗?

javascript - Next.js 表单可以在本地运行,但不能在实时服务器上运行

php - 使用 PHP 缓存 HTML 输出

php - 接受/拒绝/完成 - 数据库中记录的状态(标志)

node.js - Node Redis XREAD 阻塞订阅

javascript - 路由器级别订阅中的 meteor 计数