Skip to content

Commit

Permalink
on_reconnect callback (#972)
Browse files Browse the repository at this point in the history
* on_reconnect

* setSock() _callback(): if !on_reconnect, fall back to on_open (avoids changing default behavior)

* WebSocketApp.run_forever() sets has_done_teardown to True (see ticket 974)
  • Loading branch information
bubbleboy14 committed Mar 12, 2024
1 parent a394b46 commit 2dfecf7
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion websocket/_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ def __init__(
url: str,
header: Union[list, dict, Callable, None] = None,
on_open: Optional[Callable[[WebSocket], None]] = None,
on_reconnect: Optional[Callable[[WebSocket], None]] = None,
on_message: Optional[Callable[[WebSocket, Any], None]] = None,
on_error: Optional[Callable[[WebSocket, Any], None]] = None,
on_close: Optional[Callable[[WebSocket, Any, Any], None]] = None,
Expand Down Expand Up @@ -196,6 +197,10 @@ def __init__(
Callback object which is called at opening websocket.
on_open has one argument.
The 1st argument is this class object.
on_reconnect: function
Callback object which is called at reconnecting websocket.
on_reconnect has one argument.
The 1st argument is this class object.
on_message: function
Callback object which is called when received data.
on_message has 2 arguments.
Expand Down Expand Up @@ -246,6 +251,7 @@ def __init__(
self.cookie = cookie

self.on_open = on_open
self.on_reconnect = on_reconnect
self.on_message = on_message
self.on_data = on_data
self.on_error = on_error
Expand Down Expand Up @@ -426,6 +432,7 @@ def run_forever(
self.ping_interval = ping_interval
self.ping_timeout = ping_timeout
self.ping_payload = ping_payload
self.has_done_teardown = False
self.keep_running = True

def teardown(close_frame: ABNF = None):
Expand Down Expand Up @@ -497,7 +504,10 @@ def setSock(reconnecting: bool = False) -> None:
if self.ping_interval:
self._start_ping_thread()

self._callback(self.on_open)
if reconnecting and self.on_reconnect:
self._callback(self.on_reconnect)
else:
self._callback(self.on_open)

dispatcher.read(self.sock.sock, read, check)
except (
Expand Down

0 comments on commit 2dfecf7

Please sign in to comment.