У меня есть наблюдатель за подпиской на веб-сокеты:
Код: Выделить всё
async def run():
start = get_start_status() ### ON/OFF
while start == 1:
live = True
while live == True:
try:
async with websockets.connect(WEBSOCKET_URL, timeout=15, ping_interval=10) as websocket:
# Send subscription request
await websocket.send(json.dumps({
"jsonrpc": "2.0",
"id": 1,
"method": ###...
"params": ###...
}))
first_resp = await websocket.recv()
response_dict = json.loads(first_resp)
if 'result' in response_dict:
print("Subscription successful. Subscription ID: ", response_dict['result'])
# Continuously read from the WebSocket
async for response in websocket:
response_dict = json.loads(response)
### TO DO WiTH RESPONCES
except ConnectionTimeoutError as e:
print('Error connecting. Retrying...')
live = False
async def main():
asyncio.ensure_future(run())
asyncio.get_event_loop().run_forever()
asyncio.run(main())
Подробнее здесь: https://stackoverflow.com/questions/782 ... cket-alive