sleekxmpp pubsub的例子

时间:2012-03-16 23:46:32

标签: python xmpp publish-subscribe

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

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

任何帮助?

1 个答案:

答案 0 :(得分:9)

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

(编辑:这些文件现在位于开发分支的examples目录中。请参阅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聊天室:sleek@conference.jabber.org

- Lance