这是我在 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 个错误。我试过包括和排除一堆配置变量,比如 uuid
和 auth_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);
- 在 PubNub 管理控制台中禁用访问管理器“修复”了它,但显然我需要访问管理
- 我想我已经关注了https://www.pubnub.com/docs/web-javascript/pam-security非常准确,但没有任何效果。
- https://www.pubnub.com/developers/tutorials/access-manager/甚至没有提到
.subscribe()
,所以不知道应该做什么。 - 调用订阅(10x/秒)的 JS URL 是
http://ps9.pubnub.com/subscribe/<SUBSCRIBE_KEY>/<CHANNEL>%2C<CHANNEL>-pnpres/0/0?uuid=<UUID>&auth=<AUTH_KEY>&pnsdk=PubNub-JS-Web%2F3.7.16
为什么 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/