sleekxmpp pubsub的例子

Abh*_*ari 4 python xmpp publish-subscribe

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

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

任何帮助?

小智 9

如果你看看SleekXMPP邮件列表上的这个帖子(初学者 - SleekXMPP - XEP-0060),我有一个示例Pubsub客户端,你可以试验和检查.我将整理并抛光这两个脚本,以便很快添加到捆绑的示例中.

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

对于您的用例,您可以:

xmpp['xep_0060'].subscribe('pubsub.example.com', 'blogupdates')
Run Code Online (Sandbox Code Playgroud)

对于更高级的用法,您还可以传递:

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
Run Code Online (Sandbox Code Playgroud)

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

# 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.
Run Code Online (Sandbox Code Playgroud)

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

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']
Run Code Online (Sandbox Code Playgroud)

您可以查看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
Run Code Online (Sandbox Code Playgroud)

最后,如果您发现自己花费了太多时间在Google上搜索,请访问Sleek聊天室:sleek@conference.jabber.org

- 兰斯