首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >pubnub python 4 sdk

pubnub python 4 sdk
EN

Stack Overflow用户
提问于 2018-10-11 06:26:50
回答 1查看 495关注 0票数 3

我刚刚开始使用pubnub。我输入了(4.0)中给出的基本代码,并得到以下错误

错误:pubnub:异步请求异常。'Publish‘对象没有属性’异步‘错误:pubnub:订阅循环中的异常:’发布‘对象没有属性’异步‘警告:pubnub:重新连接策略被禁用,请手动处理重新连接。

async()而言,有一个可以解决异步错误的疑难解答:输入以下内容

代码语言:javascript
复制
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))\

但还是不起作用。

这是密码

代码语言:javascript
复制
 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()
EN

回答 1

Stack Overflow用户

发布于 2018-10-17 18:13:14

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

注意,到目前为止,这只适用于Python3,因为Python2还没有实现同样的功能。

变化

代码语言:javascript
复制
pubnub.publish().channel("awesomeChannel").message("hello!!").async(my_publish_callback)

代码语言:javascript
复制
pubnub.publish().channel("awesomeChannel").message("hello!!").pn_async(my_publish_callback)

参考文件

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52753587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档