connect_status.py 604 B

12345678910111213141516171819202122232425262728293031
  1. import asyncio
  2. from nats.aio.client import Client as NATS
  3. async def example():
  4. # [begin connect_status]
  5. nc = NATS()
  6. await nc.connect(
  7. servers=["nats://demo.nats.io:4222"],
  8. )
  9. # Do something with the connection.
  10. print("The connection is connected?", nc.is_connected)
  11. while True:
  12. if nc.is_reconnecting:
  13. print("Reconnecting to NATS...")
  14. break
  15. await asyncio.sleep(1)
  16. await nc.close()
  17. print("The connection is closed?", nc.is_closed)
  18. # [end connect_status]
  19. loop = asyncio.get_event_loop()
  20. loop.run_until_complete(example())
  21. loop.close()