WebSocket Connection¶
connection ¶
Classes¶
ConnectionBusyError ¶
Bases: WebsocketError
Raised when attempting to use a connection that is already busy.
Source code in audex/exceptions.py
ConnectionUnavailableError ¶
Bases: WebsocketError
Raised when a connection is unavailable or cannot be established.
Source code in audex/exceptions.py
ConnectionClosedError ¶
Bases: WebsocketError
Raised when attempting to use a closed connection.
Source code in audex/exceptions.py
ConnectionDrainTimeoutError ¶
Bases: WebsocketError
Raised when connection draining exceeds the timeout.
Source code in audex/exceptions.py
WebsocketConnection ¶
WebsocketConnection(*, uri: str, headers: dict[str, str] | None = None, idle_timeout: float = 30.0, check_server_data_on_release: bool = False, drain_timeout: float = 5.0, drain_condition: DrainConditionCallback | None = None, **kwargs: Any)
Bases: LoggingMixin, Hashable
Manages a single WebSocket connection with lifecycle management.
This class provides automatic idle timeout monitoring, connection health checks, and proper resource cleanup for WebSocket connections.
Attributes:
| Name | Type | Description |
|---|---|---|
uri | The WebSocket URI to connect to. | |
headers | Optional HTTP headers for the connection. | |
idle_timeout | Maximum idle time before auto-close in seconds. | |
check_server_data_on_release | Whether to check for server data on release. | |
drain_timeout | Timeout for draining server data in seconds. | |
drain_condition | Callback to determine if data should be drained. |
Initialize a WebSocket connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri | str | The WebSocket URI to connect to. | required |
headers | dict[str, str] | None | Optional HTTP headers to include in connection requests. | None |
idle_timeout | float | Maximum time in seconds before idle connection closes. Defaults to 30.0. | 30.0 |
check_server_data_on_release | bool | Whether to check for server data during release. Defaults to False. | False |
drain_timeout | float | Maximum time in seconds to drain server data. Defaults to 5.0. | 5.0 |
drain_condition | DrainConditionCallback | None | Function to determine what constitutes server data that should be drained. Defaults to None (uses default condition). | None |
**kwargs | Any | Additional parameters to pass to websockets.connect(). | {} |
Source code in audex/lib/websocket/connection.py
Attributes¶
is_busy property ¶
Check if the connection is currently busy.
Returns:
| Type | Description |
|---|---|
bool | True if the connection is busy, False otherwise. |
is_draining property ¶
Check if the connection is currently draining.
Returns:
| Type | Description |
|---|---|
bool | True if the connection is draining, False otherwise. |
is_connected property ¶
Check if the connection is currently active.
Returns:
| Type | Description |
|---|---|
bool | True if the connection is open and not closed, False otherwise. |
last_activity property ¶
Get the timestamp of the last activity.
Returns:
| Type | Description |
|---|---|
float | Unix timestamp of the last activity. |
Functions¶
connect async ¶
Establish the WebSocket connection.
If already connected, this method does nothing. Otherwise, it attempts to establish a new connection and starts the idle monitor task.
Raises:
| Type | Description |
|---|---|
ConnectionUnavailableError | If the connection has been closed or if connection establishment fails. |
Source code in audex/lib/websocket/connection.py
close async ¶
Close the WebSocket connection and clean up resources.
This method cancels the idle monitor task, closes the WebSocket connection, and resets all internal state flags.
Source code in audex/lib/websocket/connection.py
acquire async ¶
Acquire the connection for exclusive use.
This method marks the connection as busy and ensures it is connected.
Raises:
| Type | Description |
|---|---|
ConnectionUnavailableError | If the connection has been closed or if connection establishment fails. |
ConnectionBusyError | If the connection is already busy or draining. |
Source code in audex/lib/websocket/connection.py
release async ¶
Release the connection back to the pool.
This method marks the connection as no longer busy and updates the activity timestamp.
Source code in audex/lib/websocket/connection.py
ping async ¶
Send a ping to the WebSocket server to check connection health.
This method performs a health check by sending a ping frame to the server. The connection must be open for this to succeed.
Raises:
| Type | Description |
|---|---|
ConnectionUnavailableError | If the websocket is not connected. |
ConnectionClosedError | If the connection closes during the ping. |
Source code in audex/lib/websocket/connection.py
session async ¶
session() -> AsyncGenerator[WebsocketConnection, None]
Create a context manager session for the connection.
The connection is automatically acquired on entry and released on exit.
Yields:
| Type | Description |
|---|---|
AsyncGenerator[WebsocketConnection, None] | The WebsocketConnection instance. |
Example
async with connection.session(): await connection.send("Hello") response = await connection.recv()
Source code in audex/lib/websocket/connection.py
send async ¶
Send a message through the WebSocket connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message | str | bytes | The message to send (string or bytes). | required |
Raises:
| Type | Description |
|---|---|
ConnectionBusyError | If the connection has not been acquired. |
ConnectionUnavailableError | If the websocket is not connected. |
ConnectionClosedError | If the connection closes during sending. |
Source code in audex/lib/websocket/connection.py
recv async ¶
Receive a message from the WebSocket connection.
Returns:
| Type | Description |
|---|---|
str | bytes | The received message (string or bytes). |
Raises:
| Type | Description |
|---|---|
ConnectionBusyError | If the connection has not been acquired. |
ConnectionUnavailableError | If the websocket is not connected. |
ConnectionClosedError | If the connection closes during receiving. |
Source code in audex/lib/websocket/connection.py
options: show_root_heading: true show_source: true heading_level: 2 members_order: source show_signature_annotations: true separate_signature: true