Skip to content

WebSockets Reference

view.ws

WebSocketReceivable = Union[str, bytes, dict, int, bool] module-attribute

WebSocketSendable = Union[str, bytes, dict, int, bool] module-attribute

__all__ = ('WebSocketSendable', 'WebSocketReceivable', 'WebSocket') module-attribute

WebSocket

Object representing a WebSocket connection.

connect = accept class-attribute instance-attribute

done: bool = False instance-attribute

open: bool = False instance-attribute

recv = receive class-attribute instance-attribute

socket: ViewWebSocket = socket instance-attribute

__aenter__() -> Self async

__aexit__(*_) -> None async

__init__(socket: ViewWebSocket) -> None

accept() -> None async

Open the connection.

close() -> None async

Close the connection.

expect(message: WebSocketSendable) -> None async

pair(message: WebSocketSendable, *, tp: type[WebSocketReceivable] = str, recv_first: bool = False) -> WebSocketReceivable async

Receive a message and send a message.

Parameters:

Name Type Description Default
message WebSocketSendable

Message to send. Equivalent to message in send()

required
tp type[WebSocketReceivable]

Type to cast the result to. Equivalent to tp in receive()

str
recv_first bool

Whether to receive the message before sending. If this is False, a message is sent first, then a message is received.

False

receive(tp: type[WebSocketReceivable] = str) -> WebSocketReceivable async

Receive a message from the WebSocket.

Parameters:

Name Type Description Default
tp type[WebSocketReceivable]

Python type to cast the received message to.

str

send(message: WebSocketSendable) -> None async

Send a message to the client.

Parameters:

Name Type Description Default
message WebSocketSendable

Message to send.

required