Skip to content

~.websocket

Websocket

WebsocketPacket

Base websocket packet implementation.

WebsocketPacketBytes dataclass

WebsocketPacketBytes(payload: bytes)

Bases: WebsocketPacket

Bytes-structured websocket packet.

payload instance-attribute

payload: bytes

The bytes packet payload.

WebsocketPacketJSON dataclass

WebsocketPacketJSON(payload: dict[str, Any])

Bases: WebsocketPacket

JSON-structured websocket packet.

payload instance-attribute

payload: dict[str, Any]

The JSON packet payload.

WebsocketState

Bases: IntEnum

Websocket connection state.

CONNECTED class-attribute instance-attribute

CONNECTED = 0

Websocket is currently connected.

CONNECTING class-attribute instance-attribute

CONNECTING = 1

Websocket is currently connecting.

DISCONNECTED class-attribute instance-attribute

DISCONNECTED = 2

Websocket is currently disconnected.

DISCONNECTING class-attribute instance-attribute

DISCONNECTING = 3

Websocket is currently disconnecting.

Websocket

Websocket()

Custom websocket manager.

Create a new websocket.

connected property

connected: bool

If the websocket is currently connected.

connecting property

connecting: bool

If the websocket is currently connecting.

disconnected property

disconnected: bool

If the websocket is currently disconnected.

disconnecting property

disconnecting: bool

If the websocket is currently disconnecting.

connect async

connect(url: str) -> None

Connect to a websocket endpoint.

PARAMETER DESCRIPTION
url

The URL/URI to connect to.

TYPE: str

RAISES DESCRIPTION
ReconnectSignal

Error occurred and further attempts should be made to connect.

disconnect async

disconnect() -> None

Disconnect the websocket.

receive async

receive() -> WebsocketPacket

Block until a payload is received.

RETURNS DESCRIPTION
WebsocketPacket

The received payload, either bytes or JSON.

RAISES DESCRIPTION
DisconnectSignal

Error occurred and no further attempts should be made to connect.

ReconnectSignal

Error occurred and further attempts should be made to connect.

RuntimeError

An unexpected payload type was received.

ResumeSignal

Error occurred and an attempt to resume the session should be made.

send_bytes async

send_bytes(data: bytes) -> None

Send a bytes payload through the websocket.

PARAMETER DESCRIPTION
data

The bytes payload to send.

TYPE: bytes

RAISES DESCRIPTION
DisconnectSignal

Error occurred and no further attempts should be made to connect.

ReconnectSignal

Error occurred and further attempts should be made to connect.

ResumeSignal

Error occurred and an attempt to resume the session should be made.

send_json async

send_json(data: dict[str, Any]) -> None

Send a JSON payload through the websocket.

PARAMETER DESCRIPTION
data

The JSON payload to send.

TYPE: dict[str, Any]

RAISES DESCRIPTION
DisconnectSignal

Error occurred and no further attempts should be made to connect.

ReconnectSignal

Error occurred and further attempts should be made to connect.

ResumeSignal

Error occurred and an attempt to resume the session should be made.