-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket_manager.py
More file actions
42 lines (38 loc) · 1.59 KB
/
websocket_manager.py
File metadata and controls
42 lines (38 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import asyncio
import websockets
import json
from utils.utils import get_timestamp
import logging
from config.config import CONFIG
# Set up logger
logger = logging.getLogger('websocket_logger')
logger.setLevel(logging.INFO)
handler = logging.FileHandler('websocket.log')
handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s'))
logger.addHandler(handler)
async def start_websocket(url, pairs, on_data, balance):
logger.info(f"Connecting to websocket at {url}...")
try:
async with websockets.connect(url) as ws:
logger.info(f"Connected to websocket at {url}. Subscribing to pairs {pairs}...")
subscribe_message = json.dumps({
"event": "subscribe",
"pair": pairs,
"subscription": {"name": "ticker"}
})
await ws.send(subscribe_message)
logger.info(f"Sent subscription message: {subscribe_message}")
while True:
try:
data = await ws.recv()
message = json.loads(data)
await on_data(message, balance)
except websockets.ConnectionClosed:
logger.warning(f"[{get_timestamp()}] WebSocket closed, reconnecting...")
await asyncio.sleep(5)
await start_websocket(url, pairs, on_data, balance)
except Exception as e:
logger.error(f"Error in WebSocket: {str(e)}")
await asyncio.sleep(5)
except Exception as e:
logger.error(f"Failed to connect to websocket: {e}")