import asyncio
import websockets
import json
async def main():
url = "wss://ws.recluster.io/ws?password=example"
try:
async with websockets.connect(url) as ws:
print("Connected to the reCluster!")
subscription_message = {
"topic": "orderbook",
"symbol": "BTCUSDT",
"action": "subscribe"
}
await ws.send(json.dumps(subscription_message))
while True:
message = await ws.recv()
try:
data = json.loads(message)
print("Data:", data)
except json.JSONDecodeError as err:
print("Failed to parse JSON:", err, "\nRaw message:", message)
except websockets.ConnectionClosedOK:
print("Connection closed.")
except websockets.ConnectionClosedError as err:
print("Connection closed with error:", err)
except Exception as e:
print("Error occurred:", e)
if __name__ == "__main__":
asyncio.run(main())