【问题标题】:sleekxmpp pubsub exampleslimxmpp 发布订阅示例
【发布时间】:2012-04-02 12:08:59
【问题描述】:

我正在寻找实现 SleekXMPP XEP 60 插件的工作示例代码。 我想做的是 通过 TLS 进行身份验证并订阅名为“blogupdates”的节点 然后只需等待事件并获取其中包含的数据 我已经搜索了几天,但我找不到一个好的工作示例 google 或 SO

请注意,我订阅的不是用户,而是一个节点,因此发布者可以是任何人。

有什么帮助吗?

【问题讨论】:

    标签: python xmpp publish-subscribe


    【解决方案1】:

    如果您在 SleekXMPP 邮件列表 (Beginner - SleekXMPP - XEP-0060) 上查看此线程,我有一个示例 Pubsub 客户端,您可以试验和检查。我将整理和完善这两个脚本,以便很快添加到捆绑的示例中。

    (编辑:这些文件现在位于开发分支的示例目录中。参见examples/pubsub_client.pyexamples/pubsub_events.py

    对于您的用例,您会这样做:

    xmpp['xep_0060'].subscribe('pubsub.example.com', 'blogupdates')
    

    更高级的用法,也可以通过:

    bare=False
    # Subscribe using a specific resource, and not the bare JID
    
    subscribee='somejid@example.com'
    # Subscribe a different JID to the node, if authorized
    
    options=data_form
    # Provide subscription options using a XEP-0004 data form
    

    但是,如果您使用的是开发分支,我添加了一些您可能希望更轻松地处理发布事件的新功能:

    # Generic pubsub event handlers for all nodes
    xmpp.add_event_handler('pubsub_publish', handler)
    xmpp.add_event_handler('pubsub_retract', handler)
    xmpp.add_event_handler('pubsub_purge', handler)
    xmpp.add_event_handler('pubsub_delete', handler)
    
    # Use custom-named events for certain nodes, in this case 
    # the User Tune node from PEP.
    xmpp['xep_0060'].map_node_event('http://jabber.org/protocol/tune', 'user_tune')
    xmpp.add_event_handler('user_tune_publish', handler)
    # ...
    # The same suffixes as the pubsub_* events.
    # These events are raised in addition to the pubsub_* events.
    

    对于事件处理程序,您可以遵循以下模式:

    def pubsub_publish(self, msg):
        # An event message could contain multiple items, but we break them up into
        # separate events for you to make it a bit easier.
        item = msg['pubsub_event']['items']['item']
        # Process item depending on application, like item['tune'], 
        # or the generic item['payload']
    

    您可以查看 XEP-0107 和相关的 PEP 插件以查看更多示例,因为添加了这些功能来实现这些功能。

    因此,您的用例如下所示:

    # Note that xmpp can be either a ClientXMPP or ComponentXMPP object.
    xmpp['xep_0060'].map_node_event('blogupdates', 'blogupdates')
    xmpp['xep_0060'].add_event_handler('blogupdates_publish', blogupdate_publish)
    xmpp['xep_0060'].subscribe('pubsub.example.com', 'blogupdates')
    # If using a component, you'll need to specify a JID when subscribing using:   
    # ifrom="specific_jid@yourcomponent.example.com"
    
    def blogupdate_publish(msg):
        """Handle blog updates as they come in."""
        update_xml = msg['pubsub_event']['items']['item']['payload']
        # Do stuff with the ElementTree XML object, update_xml
    

    最后,如果您发现自己在 Google 上搜索的时间过多,请访问 Sleek 聊天室:slick@conference.jabber.org

    -- 兰斯

    【讨论】:

    • 非常感谢。我现在有了一些想法。我今天会试一试,当然现在我知道在哪里可以找到你们^^。绝对可以将您的优美脚本用于 pubsub,主要作为客​​户端 sub,因为这在我们想用 XMPP 替换 MQ 的 Python 相关开发中最有用,我认为这将成为不久的将来的主要用例。再次感谢
    • 它可以工作,但在我通过 pip 安装的发行版中,我有相同的 github.com/fritzy/SleekXMPP/issues/149 。 :( 还是谢谢
    • 啊,是的。 PyPI 上的 1.0 版本已经发布了几个月,并且修复了很多错误,就像您指出的那样。我们一直在等待一些插件的开发完成,但我们可能会继续进行并很快发布以修复这些错误。
    • 太棒了。我会尝试使用开发分支。我假设我的发布版本的代码将在开发中工作,并且我没有看到类和方法结构的任何变化。非常感谢,slicious 是一个很棒的库。
    • 想要发布更新。兰斯非常有帮助,谢谢兰斯,让他在 jabber conf 中得到他的帮助。其次,一切都与开发分支完美配合。我很高兴这个项目的存在。
    猜你喜欢
    • 2012-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-27
    • 2011-08-22
    • 2012-08-14
    • 1970-01-01
    相关资源
    最近更新 更多