我已经在服务器端实现了PAM,我尝试使用不同的auth密钥访问单个客户端。写访问按预期工作,但即使我授予他们读访问权限,也没有客户端收到任何消息。这是我如何初始化我的pubnub配置;
pnconfig = PNConfiguration()
pnconfig.subscribe_key = 'sub-c-...'
pnconfig.publish_key = 'pub-c-...'
pnconfig.secret_key = "sec-c-..."
pnconfig.uuid = "server"
channel = "my_channel"
pubnub = PubNub(pnconfig)
def my_publish_callback(envelope, status):
#print 'status:', status.original_response
pass
class MySubscribeCallback(SubscribeCallback):
def status(self, pubnub, status):
if status.is_error():
print("handle permissions here ERROR")
if status.category == PNStatusCategory.PNAccessDeniedCategory:
print("handle permissions here")
def message(self, pubnub, message):
global server_UUID
d = message.message
if type(d) == dict:
if (d.get('server')):
pass
else:
print 'dict>>',d
else:
print 'got>>', d
pubnub.add_listener(MySubscribeCallback())
pubnub.subscribe().channels(channel).with_presence().execute()
def input_take():
while 1:
data = raw_input("Send:")
if "yes" in data:
grant(True, True, "robot")
elif "no" in data:
grant(False, False, "robot")
def send():
c = 0
while 1:
print 'sending:', c
time.sleep(1.5)
c += 1
pubnub.publish().channel(channel).message({pubnub.uuid:str(c)}).async(my_publish_callback)
def grant(read, write, auth):
v=pubnub.grant()\
.read(read)\
.write(write)\
.channels('my_channel')\
.auth_keys(auth)\
.ttl(60)\
.sync()
#print v.result
for key, value in v.status.original_response.iteritems():
print key ,":" , value
pass
T = Thread(target=input_take)
T.start()
S = Thread(target=send)
S.start()
运行pubnub.grant()函数时,我的状态响应为200。
status : 200
message : Success
payload : {u'auths': {u'robot': {u'r': 1, u'm': 0, u'w': 1}}, u'subscribe_key': u'sub-c-...', u'ttl': 60, u'channel': u'my_channel', u'level': u'user'}
service : Access Manager
但仍然没有客户端能够接收任何消息。我正在使用pubnub调试控制台进行客户端测试并设置与服务器"机器人"相同的auth密钥。我错过了什么吗?或者我需要在PAM中配置一些东西?我使用的是PubNub 4.0.12版。
答案 0 :(得分:0)
根据我上面的评论。我在客户端添加了一个JavacScript代码,它每两秒钟就会尝试重新订阅该频道。
function try_sub(){
pubnub.subscribe({
channels: ['my_channel']
},
function (status, response) {
if (status.error) {
console.log(status)
} else {
console.log("Subscribed Success w/ timetoken", response.timetoken)
}
}
);
setTimeout('try_sub()',2000);
}