summaryrefslogtreecommitdiff
path: root/examples/pubsub_client.py
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2015-02-24 18:58:40 +0100
committermathieui <mathieui@mathieui.net>2015-02-24 22:47:15 +0100
commitc66a4d4097a249efc029b761d6150378a54bf702 (patch)
treeb25d5872f0ab8036c8b05b4207b03163f4bc7868 /examples/pubsub_client.py
parente112e864756f1222a044ee28e3c13c5925618b0c (diff)
downloadslixmpp-c66a4d4097a249efc029b761d6150378a54bf702.tar.gz
slixmpp-c66a4d4097a249efc029b761d6150378a54bf702.tar.bz2
slixmpp-c66a4d4097a249efc029b761d6150378a54bf702.tar.xz
slixmpp-c66a4d4097a249efc029b761d6150378a54bf702.zip
Update the documentation and examples
- update most of the examples with slixmpp - change the help channels pointed out in the doc - add a page listing differences from slixmpp and how to use asyncio nicely with slixmpp - fix some in-code rst documentation
Diffstat (limited to 'examples/pubsub_client.py')
-rwxr-xr-xexamples/pubsub_client.py26
1 files changed, 19 insertions, 7 deletions
diff --git a/examples/pubsub_client.py b/examples/pubsub_client.py
index fd215efd..b379af95 100755
--- a/examples/pubsub_client.py
+++ b/examples/pubsub_client.py
@@ -7,7 +7,13 @@ from argparse import ArgumentParser
import slixmpp
from slixmpp.xmlstream import ET, tostring
+from slixmpp.xmlstream.asyncio import asyncio
+def make_callback():
+ future = asyncio.Future()
+ def callback(result):
+ future.set_result(result)
+ return future, callback
class PubsubClient(slixmpp.ClientXMPP):
@@ -41,8 +47,10 @@ class PubsubClient(slixmpp.ClientXMPP):
self.disconnect()
def nodes(self):
+ future, callback = make_callback()
try:
- result = self['xep_0060'].get_nodes(self.pubsub_server, self.node)
+ self['xep_0060'].get_nodes(self.pubsub_server, self.node, callback=callback)
+ result = yield from future
for item in result['disco_items']['items']:
print(' - %s' % str(item))
except:
@@ -63,16 +71,20 @@ class PubsubClient(slixmpp.ClientXMPP):
def publish(self):
payload = ET.fromstring("<test xmlns='test'>%s</test>" % self.data)
+ future, callback = make_callback()
try:
- result = self['xep_0060'].publish(self.pubsub_server, self.node, payload=payload)
+ self['xep_0060'].publish(self.pubsub_server, self.node, payload=payload, callback=callback)
+ result = yield from future
id = result['pubsub']['publish']['item']['id']
print('Published at item id: %s' % id)
except:
logging.error('Could not publish to: %s' % self.node)
def get(self):
+ future, callback = make_callback()
try:
- result = self['xep_0060'].get_item(self.pubsub_server, self.node, self.data)
+ self['xep_0060'].get_item(self.pubsub_server, self.node, self.data, callback=callback)
+ result = yield from future
for item in result['pubsub']['items']['substanzas']:
print('Retrieved item %s: %s' % (item['id'], tostring(item['payload'])))
except:
@@ -80,28 +92,28 @@ class PubsubClient(slixmpp.ClientXMPP):
def retract(self):
try:
- result = self['xep_0060'].retract(self.pubsub_server, self.node, self.data)
+ self['xep_0060'].retract(self.pubsub_server, self.node, self.data)
print('Retracted item %s from node %s' % (self.data, self.node))
except:
logging.error('Could not retract item %s from node %s' % (self.data, self.node))
def purge(self):
try:
- result = self['xep_0060'].purge(self.pubsub_server, self.node)
+ self['xep_0060'].purge(self.pubsub_server, self.node)
print('Purged all items from node %s' % self.node)
except:
logging.error('Could not purge items from node %s' % self.node)
def subscribe(self):
try:
- result = self['xep_0060'].subscribe(self.pubsub_server, self.node)
+ self['xep_0060'].subscribe(self.pubsub_server, self.node)
print('Subscribed %s to node %s' % (self.boundjid.bare, self.node))
except:
logging.error('Could not subscribe %s to node %s' % (self.boundjid.bare, self.node))
def unsubscribe(self):
try:
- result = self['xep_0060'].unsubscribe(self.pubsub_server, self.node)
+ self['xep_0060'].unsubscribe(self.pubsub_server, self.node)
print('Unsubscribed %s from node %s' % (self.boundjid.bare, self.node))
except:
logging.error('Could not unsubscribe %s from node %s' % (self.boundjid.bare, self.node))