Last active
November 11, 2018 13:12
-
-
Save ankitml/b9d4a07af07cf8f98fd7f7947595f7c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import websockets | |
from websockets.exceptions import ConnectionClosed | |
from time import sleep | |
async def connect(): | |
while True: | |
try: | |
async with websockets.connect('ws://localhost:9999') as websocket: | |
async for message in websocket: | |
print(message) | |
except ConnectionClosed: | |
# blocking sleep | |
print('Connection closed, trying to reconnecting in few seconds') | |
sleep(5) | |
except OSError: | |
# blocking sleep | |
print('Cannot find websocket server, retrying in 15 seconds') | |
sleep(15) | |
asyncio.get_event_loop().run_until_complete(connect()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dependencies:
pip install websockets