Skip to content
This repository was archived by the owner on Jul 10, 2024. It is now read-only.

Fix #61 freeze/broken pipe after some idle time #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 18 additions & 2 deletions samsungctl/remote_websocket.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import base64
import json
import logging
import socket
import threading
import time
import select

from . import exceptions

Expand All @@ -26,9 +27,12 @@ def __init__(self, config):
self._serialize_string(config["name"]))

self.connection = websocket.create_connection(url, config["timeout"])

self._read_response()

## Keep long-lived connection alive by spawning a thread
thread = threading.Thread(target=self._keep_alive, daemon=True)
thread.start()

def __enter__(self):
return self

Expand Down Expand Up @@ -73,6 +77,18 @@ def _read_response(self):

logging.debug("Access granted.")

def _keep_alive(self):
while True:
r, w, e = select.select((self.connection.sock, ), (), ())
if not (self.connection and self.connection.connected):
# stop if connection was already closed
# while blocking in select function above
logging.debug("keep alive thread terminating.")
return
if r:
_ = self.connection.recv()
logging.debug("data received.")

@staticmethod
def _serialize_string(string):
if isinstance(string, str):
Expand Down