Fixed websocket connected

This commit is contained in:
algizn97
2025-10-06 10:27:52 +05:00
parent 5da00dbaa1
commit c687811ea5
2 changed files with 22 additions and 11 deletions

View File

@@ -22,6 +22,7 @@ class WebSocketBot:
self.ws_private = None
self.user_messages = {}
self.user_sockets = {}
self.user_keys = {}
self.loop = None
self.message_handler = TelegramMessageHandler(telegram_bot)
@@ -37,14 +38,23 @@ class WebSocketBot:
if not api_key or not api_secret:
continue
if tg_id in self.user_sockets:
keys_stored = self.user_keys.get(tg_id)
if tg_id in self.user_sockets and keys_stored == (api_key, api_secret):
continue
if tg_id in self.user_sockets:
self.user_sockets.clear()
self.user_messages.clear()
self.user_keys.clear()
logger.info("Closed old websocket for user %s due to key change", tg_id)
success = await self.try_connect_user(api_key, api_secret, tg_id)
if success:
self.user_keys[tg_id] = (api_key, api_secret)
self.user_messages.setdefault(
tg_id, {"position": None, "order": None, "execution": None}
)
logger.info("User %s connected to WebSocket", tg_id)
else:
await asyncio.sleep(30)
@@ -54,7 +64,8 @@ class WebSocketBot:
"""Clear the user_sockets and user_messages dictionaries."""
self.user_sockets.clear()
self.user_messages.clear()
logger.info("Cleared user_sockets and user_messages on shutdown")
self.user_keys.clear()
logger.info("Cleared user_sockets")
async def try_connect_user(self, api_key, api_secret, tg_id):
"""Try to connect a user to the WebSocket."""