Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion lighter/ws_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from websockets.client import connect as connect_async
from lighter.configuration import Configuration
from lighter.endpoint_profiles import join_url
import logging
logger = logging.getLogger(__name__)

class WsClient:
def __init__(
Expand All @@ -14,6 +16,8 @@ def __init__(
on_order_book_update=print,
on_account_update=print,
ws_url=None,
on_shutdown=None,
on_unhandled_message=None,
Comment thread
mihaimarcu2004 marked this conversation as resolved.
):
if ws_url is not None:
self.base_url = ws_url.rstrip("/")
Expand All @@ -35,6 +39,8 @@ def __init__(

self.on_order_book_update = on_order_book_update
self.on_account_update = on_account_update
self.on_shutdown = on_shutdown
self.on_unhandled_message = on_unhandled_message

self.ws = None

Expand All @@ -57,6 +63,8 @@ def on_message(self, ws, message):
elif message_type == "ping":
# Respond to ping with pong
ws.send(json.dumps({"type": "pong"}))
elif message_type == "shutdown":
self.handle_shutdown(message)
else:
self.handle_unhandled_message(message)

Expand Down Expand Up @@ -149,7 +157,17 @@ def handle_update_account(self, message):
self.on_account_update(account_id, self.account_states[account_id])

def handle_unhandled_message(self, message):
raise Exception(f"Unhandled message: {message}")
if self.on_unhandled_message:
self.on_unhandled_message(message)
else:
logger.warning("Unhandled message type %r: %s", message.get("type"), message)
Comment thread
mihaimarcu2004 marked this conversation as resolved.

def handle_shutdown(self, message):
close_in_ms = message.get("close_in_ms", 0)
if self.on_shutdown:
self.on_shutdown(close_in_ms)
else:
print(f"Server shutting down, connection closes in {close_in_ms}ms")
Comment thread
mihaimarcu2004 marked this conversation as resolved.

def on_error(self, ws, error):
raise Exception(f"Error: {error}")
Expand Down
Loading