From 42357d790199815783ae4dd83521908842d6a3a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mose=20M=C3=BCller?= Date: Mon, 12 Aug 2024 13:15:17 +0200 Subject: [PATCH] breaking: client takes url instead of hostname and port Connecting to secure services (with wss) was not possible. The user has to provide the whole URL now, which makes it much more flexible and less bug-prone. --- src/pydase/client/client.py | 39 ++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/pydase/client/client.py b/src/pydase/client/client.py index 537a628..17b1c6b 100644 --- a/src/pydase/client/client.py +++ b/src/pydase/client/client.py @@ -80,12 +80,12 @@ class Client: if it were local. Args: - hostname (str): - Hostname of the exposed service this client attempts to connect to. - Default is "localhost". - port (int): - Port of the exposed service this client attempts to connect on. - Default is 8001. + url (str): + The URL of the pydase Socket.IO server. This should always contain the + protocol and the hostname. + Examples: + - wss://my-service.example.com # for secure connections, use wss + - ws://localhost:8001 block_until_connected (bool): If set to True, the constructor will block until the connection to the service has been established. This is useful for ensuring the client is @@ -94,12 +94,11 @@ class Client: def __init__( self, - hostname: str, - port: int, + *, + url: str, block_until_connected: bool = True, ): - self._hostname = hostname - self._port = port + self._url = url self._sio = socketio.AsyncClient() self._loop = asyncio.new_event_loop() self.proxy = ProxyClass(sio_client=self._sio, loop=self._loop) @@ -107,29 +106,41 @@ class Client: target=asyncio_loop_thread, args=(self._loop,), daemon=True ) self._thread.start() + self.connect(block_until_connected=block_until_connected) + + def connect(self, block_until_connected: bool = True) -> None: connection_future = asyncio.run_coroutine_threadsafe( self._connect(), self._loop ) if block_until_connected: connection_future.result() + def disconnect(self) -> None: + connection_future = asyncio.run_coroutine_threadsafe( + self._disconnect(), self._loop + ) + connection_future.result() + async def _connect(self) -> None: - logger.debug("Connecting to server '%s:%s' ...", self._hostname, self._port) + logger.debug("Connecting to server '%s' ...", self._url) await self._setup_events() await self._sio.connect( - f"ws://{self._hostname}:{self._port}", + self._url, socketio_path="/ws/socket.io", transports=["websocket"], retry=True, ) + async def _disconnect(self) -> None: + await self._sio.disconnect() + async def _setup_events(self) -> None: self._sio.on("connect", self._handle_connect) self._sio.on("disconnect", self._handle_disconnect) self._sio.on("notify", self._handle_update) async def _handle_connect(self) -> None: - logger.debug("Connected to '%s:%s' ...", self._hostname, self._port) + logger.debug("Connected to '%s' ...", self._url) serialized_object = cast( SerializedDataService, await self._sio.call("service_serialization") ) @@ -141,7 +152,7 @@ class Client: self.proxy._connected = True async def _handle_disconnect(self) -> None: - logger.debug("Disconnected from '%s:%s' ...", self._hostname, self._port) + logger.debug("Disconnected from '%s' ...", self._url) self.proxy._connected = False async def _handle_update(self, data: NotifyDict) -> None: