diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2023-01-15 01:43:25 +0100 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2024-02-18 11:32:53 +0100 |
commit | 02cf9e43839ee1747c07e1f2c8c7b6ecc24518cc (patch) | |
tree | 8ea7d4d8d1d0c7e7826a7d06bb18fb8afd44d484 | |
parent | 6598b7a77c0b590fcece218b9bf065301c3ee82d (diff) | |
download | wee-slack-02cf9e43839ee1747c07e1f2c8c7b6ecc24518cc.tar.gz |
Ping websocket every 5 seconds
-rw-r--r-- | slack/init.py | 17 | ||||
-rw-r--r-- | slack/slack_workspace.py | 1 | ||||
-rw-r--r-- | typings/websocket.pyi | 3 |
3 files changed, 20 insertions, 1 deletions
diff --git a/slack/init.py b/slack/init.py index e039d9c..dd3c080 100644 --- a/slack/init.py +++ b/slack/init.py @@ -1,6 +1,9 @@ from __future__ import annotations +import socket + import weechat +from websocket import WebSocketConnectionClosedException from slack.commands import register_commands from slack.config import SlackConfig @@ -53,6 +56,19 @@ def modifier_input_text_display_with_cursor_cb( return prefix + string +def ws_ping_cb(data: str, remaining_calls: int) -> int: + for workspace in shared.workspaces.values(): + if workspace.is_connected: + try: + workspace.ws.ping() + # workspace.last_ping_time = time.time() + except (WebSocketConnectionClosedException, socket.error) as e: + # TODO: Handle error + # handle_socket_error(e, team, "ping") + print(e) + return weechat.WEECHAT_RC_OK + + async def init(): auto_connect = weechat.info_get("auto_connect", "") == "1" if auto_connect: @@ -89,5 +105,6 @@ def main(): get_callback_name(modifier_input_text_display_with_cursor_cb), "", ) + weechat.hook_timer(5000, 0, 0, get_callback_name(ws_ping_cb), "") create_task(init()) diff --git a/slack/slack_workspace.py b/slack/slack_workspace.py index fe1d5a6..87861c8 100644 --- a/slack/slack_workspace.py +++ b/slack/slack_workspace.py @@ -122,6 +122,7 @@ class SlackWorkspace: return weechat.WEECHAT_RC_OK if opcode == ABNF.OPCODE_PONG: + # TODO: Maybe record last time anything was received instead self.last_pong_time = time.time() return weechat.WEECHAT_RC_OK elif opcode != ABNF.OPCODE_TEXT: diff --git a/typings/websocket.pyi b/typings/websocket.pyi index 613d299..e7ed83b 100644 --- a/typings/websocket.pyi +++ b/typings/websocket.pyi @@ -47,10 +47,11 @@ class WebSocketConnectionClosedException(WebSocketException): ... class WebSocket: sock: socket + def ping(self, payload: str = ...) -> None: ... def recv_data( self, control_frame: bool ) -> Tuple[int, Any,]: ... def create_connection( - url: str, timeout: Optional[int], **options: Dict[str, Any] + url: str, timeout: Optional[int] = ..., **options: Dict[str, Any] ) -> WebSocket: ... |