| 1 |
ws_send(data, flags=None, fragsize=0, encoding='utf-8') -> count |
| 2 |
|
| 3 |
Send a WebSocket frame. In detached mode this requires ``CONNECT_ONLY=2``; |
| 4 |
inside an active ``WRITEFUNCTION`` callback it may also be used to send |
| 5 |
a blocking reply. |
| 6 |
|
| 7 |
*data* may be a ``str`` or any bytes-like object. ``str`` is encoded |
| 8 |
with *encoding* (UTF-8 by default); characters that are not |
| 9 |
representable in *encoding* raise ``UnicodeEncodeError``. Passing |
| 10 |
``None`` raises ``TypeError`` — use ``b""`` for an empty payload. |
| 11 |
|
| 12 |
*flags* is a bitmask built from the frame-type constants ``WS_TEXT``, |
| 13 |
``WS_BINARY``, ``WS_CONT``, ``WS_CLOSE``, ``WS_PING``, ``WS_PONG``. When |
| 14 |
``flags`` is omitted (``None``), the frame type is inferred: ``str`` -> |
| 15 |
``WS_TEXT``, bytes-like -> ``WS_BINARY``. Explicit flags win. ``str`` + |
| 16 |
``WS_BINARY`` and ``str`` + ``WS_CLOSE`` raise ``TypeError`` (use |
| 17 |
:py:meth:`ws_close` for close frames, or pass bytes-like data). |
| 18 |
|
| 19 |
*fragsize* maps to ``curl_ws_send``'s ``fragsize`` parameter; ``0`` |
| 20 |
means "whole message". ``WS_OFFSET`` is the companion flag for |
| 21 |
multi-call fragmented sends; see the libcurl docs for the rules. |
| 22 |
|
| 23 |
Returns the number of bytes accepted by libcurl. |
| 24 |
|
| 25 |
Raises ``BlockingIOError`` (``errno=EAGAIN``) in detached mode when |
| 26 |
libcurl returns ``CURLE_AGAIN``. Inside a ``WRITEFUNCTION`` callback |
| 27 |
libcurl treats the call as blocking and returns only once the frame has |
| 28 |
been fully sent; ``BlockingIOError`` does not apply. Calls from other |
| 29 |
threads while ``perform()`` is running are rejected. |
| 30 |
|
| 31 |
Corresponds to `curl_ws_send`_ in libcurl. Requires libcurl 7.86.0 or |
| 32 |
later. Raises ``pycurl.error`` for libcurl failures other than |
| 33 |
``CURLE_AGAIN``. |
| 34 |
|
| 35 |
.. _curl_ws_send: https://curl.se/libcurl/c/curl_ws_send.html |