Python PubNub 订阅者不打印消息
Python PubNub subscriber doesn't print out the message
这是我的发布者:
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
def publish_callback(result, status):
print(result)
print(status)
# Handle PNPublishResult and PNStatus
pnconfig = PNConfiguration()
pnconfig.subscribe_key = 'sub-c-ec413276-b805-11e6-b737-xxxxx'
pnconfig.publish_key = 'pub-c-528502df-76a6-4f07-8636-xxxxx'
pubnub = PubNub(pnconfig)
pubnub.publish().channel("awesomeChannel").message("hello!!").async(publish_callback)
这是我的订阅者
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
from pubnubtets import MySubscribeCallback
pnconfig = PNConfiguration()
pnconfig.subscribe_key = 'sub-c-ec413276-b805-11e6-b737-xxxxx'
pnconfig.publish_key = 'pub-c-528502df-76a6-4f07-8636-xxxxx'
pubnub = PubNub(pnconfig)
pubnub.add_listener(MySubscribeCallback())
pubnub.subscribe().channels('awesomeChannel').execute()
这是我的回调:
from pubnub.callbacks import SubscribeCallback
from pubnub.enums import PNStatusCategory
class MySubscribeCallback(SubscribeCallback):
def presence(self, pubnub, presence):
print(presence)
def status(self, pubnub, status):
if status.category == PNStatusCategory.PNUnexpectedDisconnectCategory:
pass # This event happens when radio / connectivity is lost
elif status.category == PNStatusCategory.PNConnectedCategory:
# Connect event. You can do stuff like publish, and know you'll get it.
# Or just use the connected event to confirm you are subscribed for
# UI / internal notifications, etc
pass
elif status.category == PNStatusCategory.PNReconnectedCategory:
pass
# Happens as part of our regular operation. This event happens when
# radio / connectivity is lost, then regained.
elif status.category == PNStatusCategory.PNDecryptionErrorCategory:
pass
# Handle message decryption error. Probably client configured to
# encrypt messages and on live data feed it received plain text.
def message(self, pubnub, message):
print(message)
我遇到的问题是,当我 运行 订阅者监听时,当我 运行 发布者发送消息时 hello!!
我的回调收到它,但是当我打印消息时它打印出 <pubnub.models.consumer.pubsub.PNMessageResult object at 0x108453278>
。我希望它能实际显示我的消息 hello!!
.
有什么我遗漏的吗?
来自 pubnub python sdk docs:
所以试试
print(message.message)
这是我的发布者:
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
def publish_callback(result, status):
print(result)
print(status)
# Handle PNPublishResult and PNStatus
pnconfig = PNConfiguration()
pnconfig.subscribe_key = 'sub-c-ec413276-b805-11e6-b737-xxxxx'
pnconfig.publish_key = 'pub-c-528502df-76a6-4f07-8636-xxxxx'
pubnub = PubNub(pnconfig)
pubnub.publish().channel("awesomeChannel").message("hello!!").async(publish_callback)
这是我的订阅者
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
from pubnubtets import MySubscribeCallback
pnconfig = PNConfiguration()
pnconfig.subscribe_key = 'sub-c-ec413276-b805-11e6-b737-xxxxx'
pnconfig.publish_key = 'pub-c-528502df-76a6-4f07-8636-xxxxx'
pubnub = PubNub(pnconfig)
pubnub.add_listener(MySubscribeCallback())
pubnub.subscribe().channels('awesomeChannel').execute()
这是我的回调:
from pubnub.callbacks import SubscribeCallback
from pubnub.enums import PNStatusCategory
class MySubscribeCallback(SubscribeCallback):
def presence(self, pubnub, presence):
print(presence)
def status(self, pubnub, status):
if status.category == PNStatusCategory.PNUnexpectedDisconnectCategory:
pass # This event happens when radio / connectivity is lost
elif status.category == PNStatusCategory.PNConnectedCategory:
# Connect event. You can do stuff like publish, and know you'll get it.
# Or just use the connected event to confirm you are subscribed for
# UI / internal notifications, etc
pass
elif status.category == PNStatusCategory.PNReconnectedCategory:
pass
# Happens as part of our regular operation. This event happens when
# radio / connectivity is lost, then regained.
elif status.category == PNStatusCategory.PNDecryptionErrorCategory:
pass
# Handle message decryption error. Probably client configured to
# encrypt messages and on live data feed it received plain text.
def message(self, pubnub, message):
print(message)
我遇到的问题是,当我 运行 订阅者监听时,当我 运行 发布者发送消息时 hello!!
我的回调收到它,但是当我打印消息时它打印出 <pubnub.models.consumer.pubsub.PNMessageResult object at 0x108453278>
。我希望它能实际显示我的消息 hello!!
.
有什么我遗漏的吗?
来自 pubnub python sdk docs:
所以试试
print(message.message)