Skip to content

Commit

Permalink
fix ws-client reconnection
Browse files Browse the repository at this point in the history
  • Loading branch information
51bitquant committed Jul 25, 2022
1 parent 236990a commit 72a1395
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
8 changes: 8 additions & 0 deletions howtrader/api/websocket/websocket_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ def start(self):
will call the on_connected callback when connected
subscribe the data when call the on_connected callback
"""
if self._ws:
coro = self._ws.close()
run_coroutine_threadsafe(coro, self._loop)

if self._active:
return None

self._active = True

if not self._loop:
Expand Down Expand Up @@ -198,6 +205,7 @@ async def _run(self):
self.on_error(et, ev, tb)
break

self._active = False
self.on_exit_loop()

def _record_last_sent_text(self, text: str):
Expand Down
4 changes: 2 additions & 2 deletions howtrader/gateway/binance/binance_inverse_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ def on_cancel_order(self, data: dict, request: Request) -> None:
)
self.gateway.on_order(order)

def on_cancel_order_failed(self, status_code: str, request: Request) -> None:
def on_cancel_order_failed(self, status_code: int, request: Request) -> None:
"""cancel order failed callback"""
self.failed_with_timestamp(request)
orderid = ""
Expand Down Expand Up @@ -867,7 +867,7 @@ def on_keep_user_stream(self, data: dict, request: Request) -> None:
"""extend the listen key expire time"""
self.keep_alive_failed_count = 0

def on_keep_user_stream_failed(self, status_code: str, request: Request):
def on_keep_user_stream_failed(self, status_code: int, request: Request):
self.failed_with_timestamp(request)
self.keep_alive_failed_count += 1
if self.keep_alive_failed_count <= 3:
Expand Down
4 changes: 2 additions & 2 deletions howtrader/gateway/binance/binance_spot_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ def on_start_user_stream(self, data: dict, request: Request) -> None:

self.trade_ws_api.connect(url, self.proxy_host, self.proxy_port)

def on_start_user_stream_failed(self, status_code: str, request: Request):
def on_start_user_stream_failed(self, status_code: int, request: Request):
self.failed_with_timestamp(request)
self.start_user_stream()

Expand All @@ -721,7 +721,7 @@ def on_keep_user_stream(self, data: dict, request: Request) -> None:
"""extend the listen key expire time"""
self.keep_alive_failed_count = 0

def on_keep_user_stream_failed(self, status_code: str, request: Request):
def on_keep_user_stream_failed(self, status_code: int, request: Request):
self.failed_with_timestamp(request)
self.keep_alive_failed_count += 1
if self.keep_alive_failed_count <= 3:
Expand Down
5 changes: 2 additions & 3 deletions howtrader/gateway/binance/binance_usdt_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,10 +865,9 @@ def on_start_user_stream(self, data: dict, request: Request) -> None:
self.keep_alive_count = 0

url = F_WEBSOCKET_TRADE_HOST + self.user_stream_key

self.trade_ws_api.connect(url, self.proxy_host, self.proxy_port)

def on_start_user_stream_failed(self, status_code: str, request: Request):
def on_start_user_stream_failed(self, status_code: int, request: Request):
self.failed_with_timestamp(request)
self.start_user_stream()

Expand All @@ -879,7 +878,7 @@ def on_keep_user_stream(self, data: dict, request: Request) -> None:
"""extend the listen key expire time"""
self.keep_alive_failed_count = 0

def on_keep_user_stream_failed(self, status_code: str, request: Request):
def on_keep_user_stream_failed(self, status_code: int, request: Request):
self.failed_with_timestamp(request)
self.keep_alive_failed_count += 1
if self.keep_alive_failed_count <= 3:
Expand Down

0 comments on commit 72a1395

Please sign in to comment.