Closing the mpv window now disconnects the client
This commit is contained in:
parent
d4cf649735
commit
bf2e854cdd
3 changed files with 17 additions and 13 deletions
|
@ -119,21 +119,16 @@ class State:
|
|||
class Client:
|
||||
def __init__(self, config: dict[str, Any]):
|
||||
self.sio = socketio.AsyncClient(json=jsonencoder)
|
||||
self.loop: Optional[asyncio.AbstractEventLoop] = None
|
||||
self.skipped = []
|
||||
self.sources = configure_sources(config["sources"])
|
||||
self.state = State()
|
||||
self.currentLock = asyncio.Semaphore(0)
|
||||
self.player = Player(f"{config['config']['server']}/{config['config']['room']}", self.quit)
|
||||
self.player = Player(
|
||||
f"{config['config']['server']}/{config['config']['room']}", self.quit_callback
|
||||
)
|
||||
self.register_handlers()
|
||||
|
||||
def quit(self, event):
|
||||
e = event.as_dict()
|
||||
if e["event"] == b"shutdown":
|
||||
quit()
|
||||
# if value is None:
|
||||
# return
|
||||
# print(value)
|
||||
|
||||
def register_handlers(self) -> None:
|
||||
self.sio.on("update_config", self.handle_update_config)
|
||||
self.sio.on("skip-current", self.handle_skip_current)
|
||||
|
@ -445,6 +440,10 @@ class Client:
|
|||
if self.player is not None:
|
||||
self.player.mpv.terminate()
|
||||
|
||||
def quit_callback(self):
|
||||
if self.loop is not None:
|
||||
asyncio.run_coroutine_threadsafe(self.sio.disconnect(), self.loop)
|
||||
|
||||
async def start_client(self, config: dict[str, Any]) -> None:
|
||||
"""
|
||||
Initialize the client and connect to the server.
|
||||
|
@ -454,6 +453,8 @@ class Client:
|
|||
:rtype: None
|
||||
"""
|
||||
|
||||
self.loop = asyncio.get_running_loop()
|
||||
|
||||
self.sources.update(configure_sources(config["sources"]))
|
||||
|
||||
if "config" in config:
|
||||
|
|
|
@ -30,14 +30,16 @@ class Player:
|
|||
self.default_options = {
|
||||
"scale": "bilinear",
|
||||
}
|
||||
self.quit_callback = quit_callback
|
||||
|
||||
self.mpv.observe_property("osd-width", self.osd_size_handler)
|
||||
self.mpv.observe_property("osd-height", self.osd_size_handler)
|
||||
# self.mpv.register_event_callback(quit_callback)
|
||||
# self.mpv.observe_property("exit-handler", quit_callback)
|
||||
self.mpv.register_event_callback(self.event_callback)
|
||||
|
||||
def event_printer(self, *args):
|
||||
print(args)
|
||||
def event_callback(self, event):
|
||||
e = event.as_dict()
|
||||
if e["event"] == b"shutdown":
|
||||
self.quit_callback()
|
||||
|
||||
def update_qr(self, qr_string: str) -> None:
|
||||
qr = QRCode(box_size=5, border=1)
|
||||
|
|
|
@ -38,5 +38,6 @@ class MPV:
|
|||
def loadfile(
|
||||
self, file: str, audio_file: Optional[str] = None, sub_file: Optional[str] = None
|
||||
) -> None: ...
|
||||
def register_event_callback(self, callback: Callable[..., Any]) -> None: ...
|
||||
def __setitem__(self, key: str, value: str) -> None: ...
|
||||
def __getitem__(self, key: str) -> str: ...
|
||||
|
|
Loading…
Add table
Reference in a new issue