examples: updates to reflect asyncio

This commit is contained in:
mathieui 2021-01-27 00:09:26 +01:00
parent 70b5081018
commit 9390794401
2 changed files with 12 additions and 10 deletions

View file

@ -168,13 +168,13 @@ if __name__ == '__main__':
xmpp.beClientOrServer(server=True) xmpp.beClientOrServer(server=True)
while not(xmpp.testForRelease()): while not(xmpp.testForRelease()):
xmpp.connect() xmpp.connect()
xmpp.process(block=True) xmpp.process(forever=False)
logging.debug("lost connection") logging.debug("lost connection")
if args.sensorjid: if args.sensorjid:
logging.debug("will try to call another device for data") logging.debug("will try to call another device for data")
xmpp.beClientOrServer(server=False,clientJID=args.sensorjid) xmpp.beClientOrServer(server=False,clientJID=args.sensorjid)
xmpp.connect() xmpp.connect()
xmpp.process(block=True) xmpp.process(forever=False)
logging.debug("ready ending") logging.debug("ready ending")
else: else:

View file

@ -73,21 +73,21 @@ old_xmpp = slixmpp.ClientXMPP(args.old_jid, args.old_password)
roster = [] roster = []
def on_session(event): async def on_session(event):
roster.append(old_xmpp.get_roster()) roster.append(await old_xmpp.get_roster())
old_xmpp.disconnect() old_xmpp.disconnect()
old_xmpp.add_event_handler('session_start', on_session) old_xmpp.add_event_handler('session_start', on_session)
if old_xmpp.connect(): if old_xmpp.connect():
old_xmpp.process(block=True) old_xmpp.process(forever=False)
if not roster: if not roster:
print('No roster to migrate') print('No roster to migrate')
sys.exit() sys.exit()
new_xmpp = slixmpp.ClientXMPP(args.new_jid, args.new_password) new_xmpp = slixmpp.ClientXMPP(args.new_jid, args.new_password)
def on_session2(event): async def on_session2(event):
new_xmpp.get_roster() await new_xmpp.get_roster()
new_xmpp.send_presence() new_xmpp.send_presence()
logging.info(roster[0]) logging.info(roster[0])
@ -97,9 +97,11 @@ def on_session2(event):
for jid, item in data.items(): for jid, item in data.items():
if item['subscription'] != 'none': if item['subscription'] != 'none':
new_xmpp.send_presence(ptype='subscribe', pto=jid) new_xmpp.send_presence(ptype='subscribe', pto=jid)
new_xmpp.update_roster(jid, await new_xmpp.update_roster(
name = item['name'], jid,
groups = item['groups']) name=item['name'],
groups=item['groups']
)
new_xmpp.disconnect() new_xmpp.disconnect()
new_xmpp.add_event_handler('session_start', on_session2) new_xmpp.add_event_handler('session_start', on_session2)