python-3.x - pubnub python 4 SDK

标签 python-3.x pubnub

我刚开始使用 pubnub。我输入了 pubnub python sdk (4.0) 中给出的基本代码,但出现以下错误

ERROR:pubnub:Async request Exception. 'Publish' object has no attribute 'async' ERROR:pubnub:Exception in subscribe loop: 'Publish' object has no attribute 'async' WARNING:pubnub:reconnection policy is disabled, please handle reconnection manually.

async() 而言,有一个可以解决异步错误的故障排除方法,输入以下内容

def callback(result, status):
    if status.is_error():
        print("Error %s" % str(status.error_data.exception))
        print("Error category #%d" % status.category)
    else:
        print(str(result))\

但是还是不行。

这是代码

 from pubnub.callbacks import SubscribeCallback

 from pubnub.enums import PNStatusCategory

 from pubnub.pnconfiguration import PNConfiguration

 from pubnub.pubnub import PubNub

 pnconfig = PNConfiguration()

 pnconfig.subscribe_key = 'demo'
 pnconfig.publish_key = 'demo'

 pubnub = PubNub(pnconfig)


def my_publish_callback(envelope, status):
      # Check whether request successfully completed or not
      if not status.is_error():
          pass  # Message successfully published to specified channel.
      else:
          pass  # Handle message publish error. Check 'category' property to find out possible issue
    # because of which request did fail.
    # Request can be resent using: [status retry];


 class MySubscribeCallback(SubscribeCallback):
      def presence(self, pubnub, presence):
          pass  # handle incoming presence data

      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
        pubnub.publish().channel("awesomeChannel").message("hello!!").async(my_publish_callback)
        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):
    pass  # Handle new message stored in message.message


pubnub.add_listener(MySubscribeCallback())
pubnub.subscribe().channels('awesomeChannel').execute()

最佳答案

由于错误来自发布方法,很可能是因为 async 已更改为 pn_async

请注意,到目前为止,这仅适用于 Python3,因为尚未为 Python 2 实现。

改变

pubnub.publish().channel("awesomeChannel").message("hello!!").async(my_publish_callback)

pubnub.publish().channel("awesomeChannel").message("hello!!").pn_async(my_publish_callback)

Reference document here

关于python-3.x - pubnub python 4 SDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52753587/

相关文章:

python - python setup.py egg_info mysqlclient

python - 用户警告表 Keras 2

python - fillna 没有给出预期的结果

websocket - Pusher、Pubnub 与开源 Socket.io/SignalR.net/Faye/jWebSocket

ios - 将 PubNub 集成到 Swift 框架中

django 模块导入错误 - python 2.7 与 python 3.4

python - 如何计算 Str 中的重复字符

swift - 尝试在 Pubnub 中创建 channel 时使用未声明的类型 'PNChannel'

angular - Angular5 中的 PubNub 历史

javascript - 使用 Javascript 访问在本地主机上运行的服务