Skip to content

Commit 88c71f2

Browse files
committed
Address the comments
Signed-off-by: JmPotato <[email protected]>
1 parent 44dce94 commit 88c71f2

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

hyperliquid/info.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def __init__(
2525
if not skip_ws:
2626
self.ws_manager = WebsocketManager(self.base_url)
2727
self.ws_manager.start()
28+
else:
29+
self.ws_manager = None
2830
if meta is None:
2931
meta = self.meta()
3032

@@ -43,6 +45,12 @@ def __init__(
4345
if name not in self.name_to_coin:
4446
self.name_to_coin[name] = spot_info["name"]
4547

48+
def disconnect_websocket(self):
49+
if self.ws_manager is None:
50+
raise RuntimeError("Cannot call disconnect_websocket since skip_ws was used")
51+
else:
52+
self.ws_manager.stop()
53+
4654
def user_state(self, address: str) -> Any:
4755
"""Retrieve trading details about a user.
4856
@@ -526,8 +534,3 @@ def unsubscribe(self, subscription: Subscription, subscription_id: int) -> bool:
526534

527535
def name_to_asset(self, name: str) -> int:
528536
return self.coin_to_asset[self.name_to_coin[name]]
529-
530-
def close(self):
531-
if not hasattr(self, "ws_manager") or self.ws_manager is None:
532-
return
533-
self.ws_manager.stop()

hyperliquid/websocket_manager.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,26 @@ def __init__(self, base_url):
6969
ws_url = "ws" + base_url[len("http") :] + "/ws"
7070
self.ws = websocket.WebSocketApp(ws_url, on_message=self.on_message, on_open=self.on_open)
7171
self.ping_sender = threading.Thread(target=self.send_ping)
72+
self.stop_event = threading.Event()
7273

7374
def run(self):
7475
self.ws.run_forever()
7576
self.ping_sender.start()
7677

7778
def send_ping(self):
78-
while self.ws.run_forever:
79-
time.sleep(50)
79+
while not self.stop_event.wait(50):
80+
if not self.ws.keep_running:
81+
break
8082
logging.debug("Websocket sending ping")
8183
self.ws.send(json.dumps({"method": "ping"}))
8284
logging.debug("Websocket ping sender stopped")
8385

86+
def stop(self):
87+
self.stop_event.set()
88+
self.ws.close()
89+
if self.ping_sender.is_alive():
90+
self.ping_sender.join()
91+
8492
def on_message(self, _ws, message):
8593
if message == "Websocket connection established.":
8694
logging.debug(message)
@@ -137,8 +145,3 @@ def unsubscribe(self, subscription: Subscription, subscription_id: int) -> bool:
137145
self.ws.send(json.dumps({"method": "unsubscribe", "subscription": subscription}))
138146
self.active_subscriptions[identifier] = new_active_subscriptions
139147
return len(active_subscriptions) != len(new_active_subscriptions)
140-
141-
def stop(self):
142-
if not self.ws.keep_running:
143-
return
144-
self.ws.close()

0 commit comments

Comments
 (0)