Skip to content

WebSocket

Bases: Protocol

WebSocket.

The Websocket instance can receive messages using an async iterator, until the connection is closed:

async for message in websocket:
    ...
Or directly by calling recv():
message = await websocket.recv()
Sending messages is done with send():
await websocket.send(message)

Source code in ypy_websocket/websocket.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Websocket(Protocol):
    """WebSocket.

    The Websocket instance can receive messages using an async iterator, until the connection is closed:
    ```py
    async for message in websocket:
        ...
    ```
    Or directly by calling `recv()`:
    ```py
    message = await websocket.recv()
    ```
    Sending messages is done with `send()`:
    ```py
    await websocket.send(message)
    ```
    """

    @property
    def path(self) -> str:
        """The WebSocket path."""
        ...

    def __aiter__(self):
        return self

    async def __anext__(self) -> bytes:
        try:
            message = await self.recv()
        except Exception:
            raise StopAsyncIteration()

        return message

    async def send(self, message: bytes) -> None:
        """Send a message.

        Arguments:
            message: The message to send.
        """
        ...

    async def recv(self) -> bytes:
        """Receive a message.

        Returns:
            The received message.
        """
        ...

path: str property

The WebSocket path.

recv() async

Receive a message.

Returns:

Type Description
bytes

The received message.

Source code in ypy_websocket/websocket.py
51
52
53
54
55
56
57
async def recv(self) -> bytes:
    """Receive a message.

    Returns:
        The received message.
    """
    ...

send(message) async

Send a message.

Parameters:

Name Type Description Default
message bytes

The message to send.

required
Source code in ypy_websocket/websocket.py
43
44
45
46
47
48
49
async def send(self, message: bytes) -> None:
    """Send a message.

    Arguments:
        message: The message to send.
    """
    ...