� <��g�����ddlmZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlmZmZmZmZmZddlmZmZmZmZddlmZddlmZddlmZmZm Z m!Z!m"Z"m#Z#dd l$m%Z%dd l&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0dd l1m2Z2dd lm3Z3m4Z4m5Z5d dl6m7Z7m8Z8m9Z9dgZ:Gd�dej;��Z< ddd�Z=de=_>dS)�)� annotationsN)� AsyncIterable� AsyncIterator� Awaitable�Iterable�Mapping)�Any�Callable�Deque�cast�)�asyncio_timeout)�Headers)�ConnectionClosed�ConnectionClosedError�ConnectionClosedOK� InvalidState� PayloadTooBig� ProtocolError)� Extension) �OK_CLOSE_CODES� OP_BINARY�OP_CLOSE�OP_CONT�OP_PING�OP_PONG�OP_TEXT�Close� CloseCode�Opcode)�State)�Data� LoggerLike� Subprotocol�)�Frame� prepare_ctrl� prepare_data�WebSocketCommonProtocolc�b�eZdZUdZded<dZded<ddddd d d d dddd ddd �djd%�Zdkd&�Zdkd'�Zdkd(�Z e dld)���Z e dmd*���Z e dnd+���Z e dod-���Ze dod.���Ze dpd/���Ze dpd0���Ze dmd1���Ze dld2���Zdqd4�Zdrd6�Zdsd9�Zejd:fdtd=�Zdkd>�ZdudvdB�ZdwdxdD�ZdydF�ZdkdG�ZdkdH�ZdzdI�Z d{dK�Z!d|dM�Z"d}dQ�Z#dkdR�Z$e%j&dS�d~dU�Z'duddY�Z(dkdZ�Z)dkd[�Z*dkd\�Z+dpd]�Z,ej-d:fdtd^�Z.dkd_�Z/d�db�Z0d�de�Z1dkdf�Z2dkdg�Z3d�dh�Z4dkdi�Z5dS)�r)uY WebSocket connection. :class:`WebSocketCommonProtocol` provides APIs shared between WebSocket servers and clients. You shouldn't use it directly. Instead, use :class:`~websockets.legacy.client.WebSocketClientProtocol` or :class:`~websockets.legacy.server.WebSocketServerProtocol`. This documentation focuses on low-level details that aren't covered in the documentation of :class:`~websockets.legacy.client.WebSocketClientProtocol` and :class:`~websockets.legacy.server.WebSocketServerProtocol` for the sake of simplicity. Once the connection is open, a Ping_ frame is sent every ``ping_interval`` seconds. This serves as a keepalive. It helps keeping the connection open, especially in the presence of proxies with short timeouts on inactive connections. Set ``ping_interval`` to :obj:`None` to disable this behavior. .. _Ping: https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.2 If the corresponding Pong_ frame isn't received within ``ping_timeout`` seconds, the connection is considered unusable and is closed with code 1011. This ensures that the remote endpoint remains responsive. Set ``ping_timeout`` to :obj:`None` to disable this behavior. .. _Pong: https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.3 See the discussion of :doc:`keepalive <../../topics/keepalive>` for details. The ``close_timeout`` parameter defines a maximum wait time for completing the closing handshake and terminating the TCP connection. For legacy reasons, :meth:`close` completes in at most ``5 * close_timeout`` seconds for clients and ``4 * close_timeout`` for servers. ``close_timeout`` is a parameter of the protocol because websockets usually calls :meth:`close` implicitly upon exit: * on the client side, when using :func:`~websockets.legacy.client.connect` as a context manager; * on the server side, when the connection handler terminates. To apply a timeout to any other API, wrap it in :func:`~asyncio.timeout` or :func:`~asyncio.wait_for`. The ``max_size`` parameter enforces the maximum size for incoming messages in bytes. The default value is 1 MiB. If a larger message is received, :meth:`recv` will raise :exc:`~websockets.exceptions.ConnectionClosedError` and the connection will be closed with code 1009. The ``max_queue`` parameter sets the maximum length of the queue that holds incoming messages. The default value is ``32``. Messages are added to an in-memory queue when they're received; then :meth:`recv` pops from that queue. In order to prevent excessive memory consumption when messages are received faster than they can be processed, the queue must be bounded. If the queue fills up, the protocol stops processing incoming data until :meth:`recv` is called. In this situation, various receive buffers (at least in :mod:`asyncio` and in the OS) will fill up, then the TCP receive window will shrink, slowing down transmission to avoid packet loss. Since Python can use up to 4 bytes of memory to represent a single character, each connection may use up to ``4 * max_size * max_queue`` bytes of memory to store incoming messages. By default, this is 128 MiB. You may want to lower the limits, depending on your application's requirements. The ``read_limit`` argument sets the high-water limit of the buffer for incoming bytes. The low-water limit is half the high-water limit. The default value is 64 KiB, half of asyncio's default (based on the current implementation of :class:`~asyncio.StreamReader`). The ``write_limit`` argument sets the high-water limit of the buffer for outgoing bytes. The low-water limit is a quarter of the high-water limit. The default value is 64 KiB, equal to asyncio's default (based on the current implementation of ``FlowControlMixin``). See the discussion of :doc:`memory usage <../../topics/memory>` for details. Args: logger: Logger for this server. It defaults to ``logging.getLogger("websockets.protocol")``. See the :doc:`logging guide <../../topics/logging>` for details. ping_interval: Interval between keepalive pings in seconds. :obj:`None` disables keepalive. ping_timeout: Timeout for keepalive pings in seconds. :obj:`None` disables timeouts. close_timeout: Timeout for closing the connection in seconds. For legacy reasons, the actual timeout is 4 or 5 times larger. max_size: Maximum size of incoming messages in bytes. :obj:`None` disables the limit. max_queue: Maximum number of incoming messages in receive buffer. :obj:`None` disables the limit. read_limit: High-water mark of read buffer in bytes. write_limit: High-water mark of write buffer in bytes. �bool� is_client� undefined�str�sideN�i� iF)�logger� ping_interval� ping_timeout� close_timeout�max_size� max_queue� read_limit� write_limit�host�port�secure� legacy_recv�loop�timeoutr2�LoggerLike | Noner3� float | Noner4r5r6� int | Noner7r8�intr9r:� str | Noner;r<� bool | Noner=r>� asyncio.AbstractEventLoop | Noner?�return�Nonec�v�| rtjdt��|�d}ntjdt��|�|}| �tj��} ntjdt��||_||_||_||_||_ ||_ ||_ tj ��|_ |�tjd��}tj|d|i��|_ |�tj��|_| |_| |_| |_| |_| |_tj|dz| ���|_d |_d|_tj��|_ tBj"|_#|jr|j�d ��| | | g|_$d|_% d|_&d|_'d|_(| �)��|_*tWj,��|_-d|_.d|_/d|_0i|_1d |_2 |d|_3||dS) Nzlegacy_recv is deprecated� zrename timeout to close_timeoutzremove loop argumentzwebsockets.protocol� websocketr )�limitr>Fz= connection is CONNECTINGr)4�warnings�warn�DeprecationWarning�asyncio�get_event_loopr3r4r5r6r7r8r9�uuid�uuid4�id�logging� getLogger� LoggerAdapterr2� isEnabledFor�DEBUG�debugr>�_host�_port�_securer=� StreamReader�reader�_paused� _drain_waiter�Lock� _drain_lockr!� CONNECTING�state� extensions� subprotocol� close_rcvd� close_sent�close_rcvd_then_sent� create_future�connection_lost_waiter� collections�deque�messages�_pop_message_waiter�_put_message_waiter�_fragmented_message_waiter�pings�latency�transfer_data_exc)�selfr2r3r4r5r6r7r8r9r:r;r<r=r>r?s �j/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/websockets/legacy/protocol.py�__init__z WebSocketCommonProtocol.__init__�sf��& � K� �M�5�7I� J� J� J� �?��G�G� �M�;�=O� P� P� P� � �#�M� �<��)�+�+�D�D� �M�0�2D� E� E� E�*���(���*��� �� �"���$���&���"�Z�\�\���B� �>��&�'<�=�=�F�")�"7���d�@S�"T�"T�� �)��(�(���7�7�� ��� ��� ��� ��� �&��� �*��q��t�L�L�L�� ��� �:>���"�<�>�>��� �%�� � �:� <� �K� � �:� ;� ;� ;� ��4� �%�0� �&�1�,.���/3���1�)-���(,���15��!� =A�<N�<N�<P�<P��#�&1�%6�%8�%8�� �@D�� �@D�� �HL��'�HJ�� ��� � � �3�8<��� �4� �6�6�6�c���K�|j���rtd���|jsdS|j}|�|���sJ�|j���}||_|�d{V��dS)NzConnection lost)rl�done�ConnectionResetErrorr`ra� cancelledr>rk�rv�waiters rw� _drain_helperz%WebSocketCommonProtocol._drain_helper0s����� � &� +� +� -� -� :�&�'8�9�9� 9��|� � �F��#���~��!1�!1�!3�!3�~�~�3���(�(�*�*��#���� � � � � � � � � ryc���K�|j�|j���}|�|�|j�3|j���rt jd���d{V��|����d{V��dS)Nr)r_� exception� transport� is_closingrP�sleepr��rv�excs rw�_drainzWebSocketCommonProtocol._drain<s����� �;� "��+�'�'�)�)�C���� � �>� %��~�(�(�*�*� '��m�A�&�&�&�&�&�&�&�&�&�� � �"�"�"�"�"�"�"�"�"�"�"ryc��|jtjusJ�tj|_|jr|j�d��|j�|�����|_ |j�|� ����|_ |j�|� ����|_ dS)z� Callback when the WebSocket opening handshake completes. Enter the OPEN state and start the data transfer phase. z= connection is OPENN)rer!rd�OPENrZr2r>� create_task� transfer_data�transfer_data_task�keepalive_ping�keepalive_ping_task�close_connection�close_connection_task�rvs rw�connection_openz'WebSocketCommonProtocol.connection_openLs����z�U�-�-�-�-�-��Z�� � �:� 6� �K� � �4� 5� 5� 5�"&�)�"7�"7��8J�8J�8L�8L�"M�"M���#'�9�#8�#8��9L�9L�9N�9N�#O�#O�� �%)�Y�%:�%:�4�;P�;P�;R�;R�%S�%S��"�"�"ryc�b�|jrdnd}tjd|�d�t��|jS)N�remote_address� local_address�use z[0] instead of host)r,rMrNrOr[�rv� alternatives rwr:zWebSocketCommonProtocol.host_�;��*.�.�M�&�&�o� �� �=�[�=�=�=�?Q�R�R�R��z�ryc�b�|jrdnd}tjd|�d�t��|jS)Nr�r�r�z[1] instead of port)r,rMrNrOr\r�s rwr;zWebSocketCommonProtocol.porter�ryc�D�tjdt��|jS)Nzdon't use secure)rMrNrOr]r�s rwr<zWebSocketCommonProtocol.secureks��� �(�*<�=�=�=��|�ryr c�^� |j}|�d��S#t$rYdSwxYw)a( Local address of the connection. For IPv4 connections, this is a ``(host, port)`` tuple. The format of the address depends on the address family; see :meth:`~socket.socket.getsockname`. :obj:`None` if the TCP connection isn't established yet. �socknameN�r��get_extra_info�AttributeError�rvr�s rwr�z%WebSocketCommonProtocol.local_addressr�H�� 8���I��+�+�J�7�7� 7��� � � ��4�4� ���� �� ,�,c�^� |j}|�d��S#t$rYdSwxYw)a) Remote address of the connection. For IPv4 connections, this is a ``(host, port)`` tuple. The format of the address depends on the address family; see :meth:`~socket.socket.getpeername`. :obj:`None` if the TCP connection isn't established yet. �peernameNr�r�s rwr�z&WebSocketCommonProtocol.remote_address�r�r�c�\�|jtjuo|j��� S)a{ :obj:`True` when the connection is open; :obj:`False` otherwise. This attribute may be used to detect disconnections. However, this approach is discouraged per the EAFP_ principle. Instead, you should handle :exc:`~websockets.exceptions.ConnectionClosed` exceptions. .. _EAFP: https://docs.python.org/3/glossary.html#term-eafp )rer!r�r�r{r�s rw�openzWebSocketCommonProtocol.open�s*���z�U�Z�'�N��0G�0L�0L�0N�0N�,N�Nryc�(�|jtjuS)z� :obj:`True` when the connection is closed; :obj:`False` otherwise. Be aware that both :attr:`open` and :attr:`closed` are :obj:`False` during the opening and closing sequences. )rer!�CLOSEDr�s rw�closedzWebSocketCommonProtocol.closed�s���z�U�\�)�)ryc�j�|jtjurdS|j� tjS|jjS)z� WebSocket close code, defined in `section 7.1.5 of RFC 6455`_. .. _section 7.1.5 of RFC 6455: https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.5 :obj:`None` if the connection isn't closed yet. N)rer!r�rhr�ABNORMAL_CLOSURE�coder�s rw� close_codez"WebSocketCommonProtocol.close_code�s4�� �:�U�\� )� )��4� �_� $��-� -��?�'� 'ryc�V�|jtjurdS|j�dS|jjS)z� WebSocket close reason, defined in `section 7.1.6 of RFC 6455`_. .. _section 7.1.6 of RFC 6455: https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.6 :obj:`None` if the connection isn't closed yet. N�)rer!r�rh�reasonr�s rw� close_reasonz$WebSocketCommonProtocol.close_reason�s0�� �:�U�\� )� )��4� �_� $��2��?�)� )ry�AsyncIterator[Data]c�hK� |����d{V��WV��#t$rYdSwxYw)aU Iterate on incoming messages. The iterator exits normally when the connection is closed with the close code 1000 (OK) or 1001 (going away) or without a close code. It raises a :exc:`~websockets.exceptions.ConnectionClosedError` exception when the connection is closed with any other code. TN)�recvrr�s rw� __aiter__z!WebSocketCommonProtocol.__aiter__�s`���� � (� �I�I�K�K�'�'�'�'�'�'�'�'�'�'� (��!� � � � �F�F� ���s �#� 1�1r"c��JK�|j�td���t|j��dkr�|j���}||_ t j||jgt j ����d{V��d|_n #d|_wxYw|� ��s#|j rdS|� ���d{V��t|j��dk��|j� ��}|j�!|j�d��d|_|S)a� Receive the next message. When the connection is closed, :meth:`recv` raises :exc:`~websockets.exceptions.ConnectionClosed`. Specifically, it raises :exc:`~websockets.exceptions.ConnectionClosedOK` after a normal connection closure and :exc:`~websockets.exceptions.ConnectionClosedError` after a protocol error or a network failure. This is how you detect the end of the message stream. Canceling :meth:`recv` is safe. There's no risk of losing the next message. The next invocation of :meth:`recv` will return it. This makes it possible to enforce a timeout by wrapping :meth:`recv` in :func:`~asyncio.timeout` or :func:`~asyncio.wait_for`. Returns: A string (:class:`str`) for a Text_ frame. A bytestring (:class:`bytes`) for a Binary_ frame. .. _Text: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6 .. _Binary: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6 Raises: ConnectionClosed: When the connection is closed. RuntimeError: If two coroutines call :meth:`recv` concurrently. NzPcannot call recv while another coroutine is already waiting for the next messager)� return_when)rp� RuntimeError�lenror>rkrP�waitr��FIRST_COMPLETEDr{r=� ensure_open�popleftrq� set_result)rv�pop_message_waiter�messages rwr�zWebSocketCommonProtocol.recv�sr����< � #� /��:��� ��$�-� � �A�%�%�7;�y�7N�7N�7P�7P� �'9�D� $� 0��l�'��)@�A� '� 7����������� ,0��(�(��4��(�/�/�/�/� &�*�*�,�,� -��#�-��4��*�*�,�,�,�,�,�,�,�,�,�-�$�-� � �A�%�%�2�-�'�'�)�)�� � #� /� � $� /� /�� 5� 5� 5�'+�D� $��s �-B� Br��+Data | Iterable[Data] | AsyncIterable[Data]c��T K�|����d{V��|j�&tj|j���d{V��|j�&t |t t ttf��r1t|��\}}|� d||���d{V��dSt |t��rtd���t |t���r�ttt|��}t!|��} t#|��}n#t$$rYdSwxYwt|��\}}|j���|_ |� d||���d{V��|D]K}t|��\}}||krtd���|� dt*|���d{V���L|� dt*d���d{V��n:#t,tjf$r!|�t2j���wxYw |j�d��d|_dS#|j�d��d|_wxYwt |t8���rtt:t8tgt<tft?|��j ��|��} tt:t<tgtBtft?|��j"��|���d{V��}n#tF$rYdSwxYwt|��\}}|j���|_ |� d||���d{V��|23d{V��}t|��\}}||krtd���|� dt*|���d{V���Q6|� dt*d���d{V��n:#t,tjf$r!|�t2j���wxYw |j�d��d|_dS#|j�d��d|_wxYwtd���)aZ Send a message. A string (:class:`str`) is sent as a Text_ frame. A bytestring or bytes-like object (:class:`bytes`, :class:`bytearray`, or :class:`memoryview`) is sent as a Binary_ frame. .. _Text: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6 .. _Binary: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6 :meth:`send` also accepts an iterable or an asynchronous iterable of strings, bytestrings, or bytes-like objects to enable fragmentation_. Each item is treated as a message fragment and sent in its own frame. All items must be of the same type, or else :meth:`send` will raise a :exc:`TypeError` and the connection will be closed. .. _fragmentation: https://datatracker.ietf.org/doc/html/rfc6455#section-5.4 :meth:`send` rejects dict-like objects because this is often an error. (If you want to send the keys of a dict-like object as fragments, call its :meth:`~dict.keys` method and pass the result to :meth:`send`.) Canceling :meth:`send` is discouraged. Instead, you should close the connection with :meth:`close`. Indeed, there are only two situations where :meth:`send` may yield control to the event loop and then get canceled; in both cases, :meth:`close` has the same effect and is more clear: 1. The write buffer is full. If you don't want to wait until enough data is sent, your only alternative is to close the connection. :meth:`close` will likely time out then abort the TCP connection. 2. ``message`` is an asynchronous iterator that yields control. Stopping in the middle of a fragmented message will cause a protocol error and the connection will be closed. When the connection is closed, :meth:`send` raises :exc:`~websockets.exceptions.ConnectionClosed`. Specifically, it raises :exc:`~websockets.exceptions.ConnectionClosedOK` after a normal connection closure and :exc:`~websockets.exceptions.ConnectionClosedError` after a protocol error or a network failure. Args: message: Message to send. Raises: ConnectionClosed: When the connection is closed. TypeError: If ``message`` doesn't have a supported type. NTzdata is a dict-like objectFz data contains inconsistent typesryz)data must be str, bytes-like, or iterable)$r�rrrP�shield� isinstancer.�bytes� bytearray� memoryviewr(� write_framer� TypeErrorrr r"�iter�next� StopIterationr>rkr� Exception�CancelledError�fail_connectionr�INTERNAL_ERRORr�rr r�typer�r� __anext__�StopAsyncIteration)rvr��opcode�data� iter_message�fragment�confirm_opcode� aiter_messages rw�sendzWebSocketCommonProtocol.send6s�����l��� � � � � � � � � ��-�9��.��!@�A�A� A� A� A� A� A� A� A��-�9� �g��U�I�z�B� C� C�] I�'��0�0�L�F�D��"�"�4���6�6� 6� 6� 6� 6� 6� 6� 6� 6� 6���� )� )�W I��8�9�9� 9���� *� *�R I��8�D�>�7�3�3�G���=�=�L� �� �-�-���� � � � ���� ����'��1�1�L�F�D�.2�i�.E�.E�.G�.G�D� +� 7��&�&�u�f�d�;�;�;�;�;�;�;�;�;�!-�A�A�H�+7��+A�+A�(�N�D�%��/�/�'�(J�K�K�K��*�*�5�'�4�@�@�@�@�@�@�@�@�@�@��&�&�t�W�c�:�:�:�:�:�:�:�:�:�:���w�5�6� � � ��$�$�Y�%=�>�>�>��  ����;��/�:�:�4�@�@�@�26��/�/�/���/�:�:�4�@�@�@�26��/�6�6�6�6���� /� /�, I��D��-��-�.� �d�0C�C�D��W� � �'������M� �"���m�D�1�2�I�d�O�C�D���'�'�1�"�"� �"!�"!�!�!�!�!�!�!����&� � � ���� ����'��1�1�L�F�D�.2�i�.E�.E�.G�.G�D� +� 7��&�&�u�f�d�;�;�;�;�;�;�;�;�;�'4�A�A�A�A�A�A�A�(�+7��+A�+A�(�N�D�%��/�/�'�(J�K�K�K��*�*�5�'�4�@�@�@�@�@�@�@�@�@�@� '4��&�&�t�W�c�:�:�:�:�:�:�:�:�:�:���w�5�6� � � ��$�$�Y�%=�>�>�>��  ����;��/�:�:�4�@�@�@�26��/�/�/���/�:�:�4�@�@�@�26��/�6�6�6�6��G�H�H� Hso� D� D+�*D+�B G-�,I �-7H$�$I � #I.�!AM� M�M�P�"O3�(A.P�Q5�7Q�Q5�5#Rr�r�r�c��HK� t|j��4�d{V��|�t||�����d{V��ddd���d{V��n#1�d{V��swxYwYn)#tj$r|���YnwxYw t|j��4�d{V��|j�d{V��ddd���d{V��n#1�d{V��swxYwYn!#tjtjf$rYnwxYwt j |j ���d{V��dS)ae Perform the closing handshake. :meth:`close` waits for the other end to complete the handshake and for the TCP connection to terminate. As a consequence, there's no need to await :meth:`wait_closed` after :meth:`close`. :meth:`close` is idempotent: it doesn't do anything once the connection is closed. Wrapping :func:`close` in :func:`~asyncio.create_task` is safe, given that errors during connection termination aren't particularly useful. Canceling :meth:`close` is discouraged. If it takes too long, you can set a shorter ``close_timeout``. If you don't want to wait, let the Python process exit, then the OS will take care of closing the TCP connection. Args: code: WebSocket close code. reason: WebSocket close reason. N) rr5�write_close_framerrP� TimeoutErrorr�r�r�r�r�)rvr�r�s rw�closezWebSocketCommonProtocol.close�s�����8 #�&�t�'9�:�:� B� B� B� B� B� B� B� B��,�,�U�4��-@�-@�A�A�A�A�A�A�A�A�A� B� B� B� B� B� B� B� B� B� B� B� B� B� B� B� B� B� B� B� B� B� B� B���� B� B� B� B����#� #� #� #� � � � "� "� "� "� "�  #���� �'�t�'9�:�:� .� .� .� .� .� .� .� .��-�-�-�-�-�-�-�-� .� .� .� .� .� .� .� .� .� .� .� .� .� .� .� .� .� .� .� .� .� .� .���� .� .� .� .����$�g�&<�=� � � � �D� �����n�T�7�8�8�8�8�8�8�8�8�8�8�8sj�A,�*A�A,� A$�$A,�'A$�(A,�,#B�B�C"�0C�>C"� C�C"�C�C"�"D�?Dc��HK�tj|j���d{V��dS)a9 Wait until the connection is closed. This coroutine is identical to the :attr:`closed` attribute, except it can be awaited. This can make it easier to detect connection termination, regardless of its cause, in tasks that interact with the WebSocket connection. N)rPr�rlr�s rw� wait_closedz#WebSocketCommonProtocol.wait_closed s3�����n�T�8�9�9�9�9�9�9�9�9�9�9�9ryr�� Data | None�Awaitable[float]c���K�|����d{V��|�t|��}||jvrtd���|� ||jvr2t jdt jd����}|�)||jv�2|j� ��}tj ��}||f|j|<|� dt|���d{V��tj|��S)aW Send a Ping_. .. _Ping: https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.2 A ping may serve as a keepalive, as a check that the remote endpoint received all messages up to this point, or to measure :attr:`latency`. Canceling :meth:`ping` is discouraged. If :meth:`ping` doesn't return immediately, it means the write buffer is full. If you don't want to wait, you should close the connection. Canceling the :class:`~asyncio.Future` returned by :meth:`ping` has no effect. Args: data: Payload of the ping. A string will be encoded to UTF-8. If ``data`` is :obj:`None`, the payload is four random bytes. Returns: A future that will be completed when the corresponding pong is received. You can ignore it if you don't intend to wait. The result of the future is the latency of the connection in seconds. :: pong_waiter = await ws.ping() # only if you want to wait for the corresponding pong latency = await pong_waiter Raises: ConnectionClosed: When the connection is closed. RuntimeError: If another ping was sent with the same data and the corresponding pong wasn't received yet. Nz-already waiting for a pong with the same dataz!Ir1T)r�r'rsr��struct�pack�random� getrandbitsr>rk�time� perf_counterr�rrPr�)rvr�� pong_waiter�ping_timestamps rw�pingzWebSocketCommonProtocol.pings����J��� � � � � � � � � � � ���%�%�D� �4�:� � ��N�O�O� O��l�d�d�j�0�0��;�t�V�%7��%;�%;�<�<�D��l�d�d�j�0�0��i�-�-�/�/� ��*�,�,��'��8�� �4�����t�W�d�3�3�3�3�3�3�3�3�3��~�k�*�*�*ryryc��K�|����d{V��t|��}|�dt|���d{V��dS)a' Send a Pong_. .. _Pong: https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.3 An unsolicited pong may serve as a unidirectional heartbeat. Canceling :meth:`pong` is discouraged. If :meth:`pong` doesn't return immediately, it means the write buffer is full. If you don't want to wait, you should close the connection. Args: data: Payload of the pong. A string will be encoded to UTF-8. Raises: ConnectionClosed: When the connection is closed. NT)r�r'r�r�rvr�s rw�pongzWebSocketCommonProtocol.pongTsi����&��� � � � � � � � � ��D�!�!�����t�W�d�3�3�3�3�3�3�3�3�3�3�3ryrc��|j�N|jjtvr;|j�4|jjtvr!t |j|j|j��}n t |j|j|j��}|j|_|S�N) rhr�rrirrjrru� __cause__r�s rw�connection_closed_excz-WebSocketCommonProtocol.connection_closed_excos��� �O� '���$��6�6���+���$��6�6�$������)���C�C� (������)���C� �.�� �� ryc���K�|jtjurN|j���r3t j|j���d{V��|����dS|jtj ur|����|jtj ur3t j|j���d{V��|����|jtj usJ�td���)z� Check that the WebSocket connection is open. Raise :exc:`~websockets.exceptions.ConnectionClosed` if it isn't. Nz*WebSocket connection isn't established yet) rer!r�r�r{rPr�r�r�r��CLOSINGrdrr�s rwr�z#WebSocketCommonProtocol.ensure_open�s����� �:��� #� #��&�+�+�-�-� ��n�T�%?�@�@�@�@�@�@�@�@�@��0�0�2�2�2��� �:��� %� %��,�,�.�.� .� �:��� &� &� �.��!;�<�<� <� <� <� <� <� <� <��,�,�.�.� .��z�U�-�-�-�-�-��G�H�H�Hryc���K� |����d{V��}|�dS|j��t|j��|jkro|j���|_ tj|j���d{V��d|_n #d|_wxYwt|j��|jk�o|j� |��|j �!|j � d��d|_ ��#tj $r }||_ �d}~wt$r1}||_ |�t j��Yd}~dSd}~wt$t&t(t*jf$r1}||_ |�t j��Yd}~dSd}~wt0$r1}||_ |�t j��Yd}~dSd}~wt4$r1}||_ |�t j��Yd}~dSd}~wt8$rM}|j�dd���||_ |�t j��Yd}~dSd}~wwxYw)z� Read incoming messages and put them in a queue. This coroutine runs in a task until the closing handshake is started. TNzdata transfer failed��exc_info) � read_messager7r�ror>rkrqrPr��appendrpr�r�rurr�r�PROTOCOL_ERROR�ConnectionErrorr��EOFError�ssl�SSLErrorr��UnicodeDecodeError� INVALID_DATAr�MESSAGE_TOO_BIGr�r2�errorr�)rvr�r�s rwr�z%WebSocketCommonProtocol.transfer_data�s�����= ;� 4� $� 1� 1� 3� 3�3�3�3�3�3�3���?��E��>�-��d�m�,�,���>�>�37�9�3J�3J�3L�3L��0�<�")�.��1I�"J�"J�J�J�J�J�J�J�J�7;�D�4�4��t�D�4�;�;�;�;� �d�m�,�,���>�>�� �$�$�W�-�-�-��+�7��,�7�7��=�=�=�/3�D�,�- 4��0�%� � � �%(�D� "� ������ ;� ;� ;�%(�D� "� � � ��!9� :� :� :� :� :� :� :� :� :�������x���F� =� =� =�&)�D� "� � � ��!;� <� <� <� <� <� <� <� <� <�����!� 9� 9� 9�%(�D� "� � � ��!7� 8� 8� 8� 8� 8� 8� 8� 8� 8������ <� <� <�%(�D� "� � � ��!:� ;� ;� ;� ;� ;� ;� ;� ;� ;������ ;� ;� ;� �K� � �4�t� � D� D� D�%(�D� "� � � ��!9� :� :� :� :� :� :� :� :� :����� ;���ss�C9�AC9�&B �C9� B�A#C9�9I&�D� I&�&E � %I&�.&F� I&�'&G� I&� &H � I&�AI!�!I&c������K�|�|j����d{V��}|�dS|jtkrd}n"|jtkrd}nt d���|jr"|r|j���n|jSg�|j�|r3tj d��}|d������d��fd � }nd���fd� }n��d�fd� }nd��fd� }||��|js^|������d{V��}|�t d���|jtkrt d���||��|j�^|rdnd� ���S)z� Read a single message from the connection. Re-assemble data frames if the message is fragmented. Return :obj:`None` when the closing handshake is started. )r6NTFzunexpected opcodezutf-8�strict)�errors�framer&rGrHc�n������|j|j����dSr�)r��decoder��fin)r �decoder� fragmentss ��rwr�z4WebSocketCommonProtocol.read_message.<locals>.appends/����$�$�W�^�^�E�J�� �%J�%J�K�K�K�K�Kryc��������|j|j����t �t ��sJ��t |j��z�dSr�)r�r r�r r�rCr�)r r rr6s ���rwr�z4WebSocketCommonProtocol.read_message.<locals>.appendsU����$�$�W�^�^�E�J�� �%J�%J�K�K�K�%�h��4�4�4�4�4���E�J���/�H�H�Hryc�<����|j��dSr�)r�r�)r rs �rwr�z4WebSocketCommonProtocol.read_message.<locals>.appends����$�$�U�Z�0�0�0�0�0ryc�����|j��t�t��sJ��t |j��z�dSr�)r�r�r�rCr�)r rr6s ��rwr�z4WebSocketCommonProtocol.read_message.<locals>.append%sE����$�$�U�Z�0�0�0�%�h��4�4�4�4�4���E�J���/�H�H�Hryzincomplete fragmented messager�ry)r r&rGrH) �read_data_framer6r�rrrr r�r �codecs�getincrementaldecoderr�join)rvr �text�decoder_factoryr�r rr6s @@@rwr�z$WebSocketCommonProtocol.read_message�s6��������*�*�D�M�*�B�B�B�B�B�B�B�B�� �=��4� �<�7� "� "��D�D� �\�Y� &� &��D�D�� 3�4�4� 4� �9� ?�*.�>�5�:�$�$�&�&�&�E�J� >�!#� ��=�� � 0�$�:�7�C�C�O�%�o�X�6�6�6�G���L�L�L�L�L�L�L�L� 0�0�0�0�0�0�0�0�0���1�1�1�1�1�1�1� 0�0�0�0�0�0�0� ��u� � � ��)� ��.�.��.�A�A�A�A�A�A�A�A�E��}�#�$C�D�D�D��|�w�&�&�#�$7�8�8�8� �F�5�M�M�M� �)� ��#����)�)�)�4�4�4ry� Frame | Nonec��vK� |�|���d{V��}|jtkrftj|j��|_|j�d|_ |� |j|j���d{V��n#t$rYnwxYwdS|jtkrF|j tjur2 |�|j���d{V��n�#t$rYn�wxYwn�|jt kr�|j|jvr�t%j��}d}g}|j���D]`\}\}}|�|��|���s|�||z ��||jkr ||z |_n�at3d���|D] }|j|=� n|S���)z� Read a single data frame from the connection. Process control frames received before the next data frame. Return :obj:`None` if a close frame is encountered before any data frame. TNFz!solicited pong not found in pings)� read_framer�rr�parser�rhrirjr�rrrer!r�r�rrsr�r��itemsr�r{r�rt�AssertionError)rvr6r �pong_timestamp�ping_id�ping_idsr�r�s rwrz'WebSocketCommonProtocol.read_data_frame7s-����3 ��/�/�(�3�3�3�3�3�3�3�3�E��|�x�'�'�#(�+�e�j�"9�"9����?�.�05�D�-���0�0���%�*�M�M�M�M�M�M�M�M�M�M��'�����D������t����(�(��:���+�+��"�i�i�� �3�3�3�3�3�3�3�3�3�3��+����������,����(�(��:���+�+�%)�%6�%8�%8�N�#�G�!�H�BF�*�BR�BR�BT�BT�R�R�>��!>�+�~� ����0�0�0�*�/�/�1�1�T�'�2�2�>�N�3R�S�S�S�"�e�j�0�0�+9�N�+J�D�L�!�E�1�-�-P�Q�Q�Q�#+�0�0�� �J�w�/�/��� �g3 s$�&B� B�B�9 C� C'�&C'r&c��K�tj|jj|j ||j����d{V��}|jr|j�d|��|S)z; Read a single frame from the connection. )�maskr6rfNz< %s)r&�readr_� readexactlyr,rfrZr2)rvr6r s rwrz"WebSocketCommonProtocol.read_framevs}���� �j� �K� #��^�#����  � � � � � � � � �� �:� -� �K� � �f�e� ,� ,� ,�� ryr r�r�c���t|t|��|��}|jr|j�d|��|�|jj|j|j���dS)Nz> %s)r"rf)r&r rZr2�writer�r,rf)rvr r�r�r s rw�write_frame_syncz(WebSocketCommonProtocol.write_frame_sync�sr���c�6�&�>�>�4�0�0�� �:� -� �K� � �f�e� ,� ,� ,� � � � �N� ����� � � � � � ryc��"K� |j4�d{V��|����d{V��ddd���d{V��dS#1�d{V��swxYwYdS#t$r2|���|����d{V��YdSwxYwr�)rcr�r�r�r�r�s rw�drainzWebSocketCommonProtocol.drain�sR���� %��'� $� $� $� $� $� $� $� $��k�k�m�m�#�#�#�#�#�#�#� $� $� $� $� $� $� $� $� $� $� $� $� $� $� $� $� $� $� $� $� $� $� $� $���� $� $� $� $� $� $��� %� %� %� � � � "� "� "��"�"�$�$� $� $� $� $� $� $� $� $� $� $�  %���s1� A�?�A� A � A� A � A�8B� B��_stater+c��K�|j|urtd|jj�d����|�|||��|����d{V��dS)Nz#Cannot write to a WebSocket in the z state)rer�namer'r))rvr r�r�r+s rwr�z#WebSocketCommonProtocol.write_frame�sy���� �:�V� #� #��M�d�j�o�M�M�M��� � ���c�6�4�0�0�0��j�j�l�l���������ryr�r� bytes | Nonec��JK�|jtjur�tj|_|jr|j�d��||_|j�d|_|�|� ��}|� dt|tj����d{V��dSdS)z� Write a close frame if and only if the connection state is OPEN. This dedicated coroutine must be used for writing close frames to ensure that at most one close frame is sent on a given connection. �= connection is CLOSINGNTr*) rer!r�r�rZr2rirhrj� serializer�r)rvr�r�s rwr�z)WebSocketCommonProtocol.write_close_frame�s����� �:��� #� #���D�J��z� =�� �!�!�";�<�<�<�#�D�O���*�,0��)��|����(�(���"�"�4��4�� �"�N�N� N� N� N� N� N� N� N� N� N� $� #ryc��K�|j�dS tj|j���d{V��|j�d��|����d{V��}|j�� t|j��4�d{V��|�d{V��ddd���d{V��n#1�d{V��swxYwY|j�d��nW#tj$rE|jr|j�d��|� tj d��YdSwxYw��#t$rYdSt$r |j�dd���YdSwxYw) a> Send a Ping frame and wait for a Pong frame at regular intervals. This coroutine exits when the connection terminates and one of the following happens: - :meth:`ping` raises :exc:`ConnectionClosed`, or - :meth:`close_connection` cancels :attr:`keepalive_ping_task`. NTz% sending keepalive pingz% received keepalive pongz&- timed out waiting for keepalive pongzkeepalive ping timeoutzkeepalive ping failedr�)r3rPr�r2rZr�r4rr�r�rr�rr�r)rvr�s rwr�z&WebSocketCommonProtocol.keepalive_ping�sN���� � � %� �F� F� ��m�D�$6�7�7�7�7�7�7�7�7�7�� �!�!�"<�=�=�=�$(�I�I�K�K�/�/�/�/�/�/� ��$�0��#2�4�3D�#E�#E�.�.�.�.�.�.�.�.� #.�-�-�-�-�-�-�-� .�.�.�.�.�.�.�.�.�.�.�.�.�.�.�.�.�.�.�.�.�.�.����.�.�.�.� � �)�)�*E�F�F�F�F��"�/�����:�X� �K�-�-�.V�W�W�W��,�,�%�4�4����������� ��0 � � � � �D�D�� F� F� F� �K� � �5�� � E� E� E� E� E� E� F���sa�AD#�)C � B� C � B(�(C �+B(�,C � D#� AD�D#�D�D#�# E�0%E�Ec��K� t|d��r$ |j�d{V��n#tj$rYnwxYwt|d��r|j���|jrht|d��rX|����d{V��r |����d{V��dS|j r|j � d��|j � ��r�|j r|j � d�� |j � ��n#ttf$rYnwxYw|����d{V��r |����d{V��dS|j r|j � d��|����d{V��dS#|����d{V��wxYw)a� 7.1.1. Close the WebSocket Connection When the opening handshake succeeds, :meth:`connection_open` starts this coroutine in a task. It waits for the data transfer phase to complete then it closes the TCP connection cleanly. When the opening handshake fails, :meth:`fail_connection` does the same. There's no data transfer phase in that case. r�Nr��!- timed out waiting for TCP closezx half-closing TCP connection)�hasattrr�rPr�r��cancelr,�wait_for_connection_lost�close_transportrZr2r�� can_write_eof� write_eof�OSErrorr�r�s rwr�z(WebSocketCommonProtocol.close_connection�st����( )��t�1�2�2� ���1�1�1�1�1�1�1�1�1���-�����D������t�2�3�3� 2��(�/�/�1�1�1��~� K�'�$�0D�"E�"E� K��6�6�8�8�8�8�8�8�8�8���2�&�&�(�(� (� (� (� (� (� (� (� (� (�1�:�K��K�%�%�&I�J�J�J��~�+�+�-�-� K��:�G��K�%�%�&E�F�F�F� ��N�,�,�.�.�.�.����.�����D������6�6�8�8�8�8�8�8�8�8����&�&�(�(� (� (� (� (� (� (� (� (� (� �:�K��K�%�%�&I�J�J�J� �&�&�(�(� (� (� (� (� (� (� (� (� (��$�&�&�(�(� (� (� (� (� (� (� (� (���sX�F0� #�F0�5�F0�5�AF0�/AF0� D%�$F0�%D9�6F0�8D9�9F0�3!F0�0G c��K�|j���r|j���rdS|jr|j�d��|j���|����d{V��rdS|jr|j�d��|jr|j�d��|j���|����d{V��dS)z, Close the TCP connection. Nzx closing TCP connectionr4zx aborting TCP connection) rlr{r�r�rZr2r�r7�abortr�s rwr8z'WebSocketCommonProtocol.close_transport%s ���� � &� +� +� -� -� �$�.�2K�2K�2M�2M� � �F� �:� :� �K� � �8� 9� 9� 9� ��������.�.�0�0� 0� 0� 0� 0� 0� 0� � �F� �:� C� �K� � �A� B� B� B� �:� ;� �K� � �9� :� :� :� ��������+�+�-�-�-�-�-�-�-�-�-�-�-ryc��RK�|j���st t|j��4�d{V��t j|j���d{V��ddd���d{V��n#1�d{V��swxYwYn#tj$rYnwxYw|j���S)z� Wait until the TCP connection is closed or ``self.close_timeout`` elapses. Return :obj:`True` if the connection is closed and :obj:`False` otherwise. N)rlr{rr5rPr�r�r�s rwr7z0WebSocketCommonProtocol.wait_for_connection_lostBsg�����*�/�/�1�1� � �*�4�+=�>�>�F�F�F�F�F�F�F�F�!�.��)D�E�E�E�E�E�E�E�E�E�F�F�F�F�F�F�F�F�F�F�F�F�F�F�F�F�F�F�F�F�F�F�F����F�F�F�F����'� � � ��� ���� �*�/�/�1�1�1s4�A;� A)�A;�) A3�3A;�6A3�7A;�;B � B c�h�|jr|j�d|��t|d��r|j���|t jkr�|jtj ur�t||��}tj |_|jr|j�d��|j �J�||_ |�dt|�����t|d��s3|j�|�����|_dSdS)a� 7.1.7. Fail the WebSocket Connection This requires: 1. Stopping all processing of incoming data, which means cancelling :attr:`transfer_data_task`. The close code will be 1006 unless a close frame was received earlier. 2. Sending a close frame with an appropriate code if the opening handshake succeeded and the other side is likely to process it. 3. Closing the connection. :meth:`close_connection` takes care of this once :attr:`transfer_data_task` exits after being canceled. (The specification describes these steps in the opposite order.) z!! failing connection with code %dr�r0NTr�)rZr2r5r�r6rr�rer!r�rr�rhrir'rr1r>r�r�r�)rvr�r�r�s rwr�z'WebSocketCommonProtocol.fail_connectionUs0��. �:� I� �K� � �A�4� H� H� H� �4�-� .� .� -� � #� *� *� ,� ,� ,� �9�-� -� -�$�*�� �2J�2J��$��'�'�E���D�J��z� =�� �!�!�";�<�<�<� �?�*�*�*�#�D�O� � !� !�$��%�/�/�2C�2C� D� D� D��t�4�5�5� X�)-��)>�)>�t�?T�?T�?V�?V�)W�)W�D� &� &� &� X� Xryc���|jtjusJ�|���}|j���D].\}}|�|��|����/dS)z� Raise ConnectionClosed in pending keepalive pings. They'll never receive a pong once the connection is closed. N)rer!r�r�rs�values� set_exceptionr6)rvr�r��_ping_timestamps rw� abort_pingsz#WebSocketCommonProtocol.abort_pings�s����z�U�\�)�)�)�)��(�(�*�*��,0�J�,=�,=�,?�,?� !� !� (�K�� � %� %�c� *� *� *� � � � � � � �  !� !ryr��asyncio.BaseTransportc��ttj|��}|�|j��||_|j�|��dS)a� Configure write buffer limits. The high-water limit is defined by ``self.write_limit``. The low-water limit currently defaults to ``self.write_limit // 4`` in :meth:`~asyncio.WriteTransport.set_write_buffer_limits`, which should be all right for reasonable use cases of this library. This is the earliest point where we can get hold of the transport, which means it's the best point for configuring it. N)r rP� Transport�set_write_buffer_limitsr9r�r_� set_transportr�s rw�connection_madez'WebSocketCommonProtocol.connection_made�sR����*�I�6�6� ��)�)�$�*:�;�;�;�"��� � �!�!�)�,�,�,�,�,ryr��Exception | Nonec���tj|_|j�d��|���|j�d�� |j�6|�|j� ��n|j� |��|j sdS|j }|�dSd|_ |� ��rdS|�|�d��dS|� |��dS)z= 7.1.4. The WebSocket Connection is Closed. z= connection is CLOSEDN)r!r�rer2rZrDrlr�r_�feed_eofrBr`rar{)rvr�rs rw�connection_lostz'WebSocketCommonProtocol.connection_lost�s�� �\�� � � ���2�3�3�3� ������ �#�.�.�t�4�4�4� *��{�&��;��K�(�(�*�*�*�*��K�-�-�c�2�2�2��<� ����'�F��~���!%�D� ��{�{�}�}� ����{��!�!�$�'�'�'�'�'��$�$�S�)�)�)�)�)ryc�&�|jrJ�d|_dS)NT)r`r�s rw� pause_writingz%WebSocketCommonProtocol.pause_writing�s���<������ � � ryc��|jsJ�d|_|j}|�2d|_|���s|�d��dSdSdS)NF)r`rar{r�r~s rw�resume_writingz&WebSocketCommonProtocol.resume_writing�sh���|���|��� ��#�� � �!%�D� ��;�;�=�=� (��!�!�$�'�'�'�'�'� � � (� (ryc�:�|j�|��dSr�)r_� feed_datar�s rw� data_receivedz%WebSocketCommonProtocol.data_received�s�� � ���d�#�#�#�#�#ryc�8�|j���dS)a� Close the transport after receiving EOF. The WebSocket protocol has its own closing handshake: endpoints close the TCP or TLS connection after sending and receiving a close frame. As a consequence, they never need to write after receiving EOF, so there's no reason to keep the transport open by returning :obj:`True`. Besides, that doesn't work on TLS connections. N)r_rMr�s rw� eof_receivedz$WebSocketCommonProtocol.eof_received�s�� � �������ry)r2r@r3rAr4rAr5rAr6rBr7rBr8rCr9rCr:rDr;rBr<rEr=r+r>rFr?rArGrH)rGrH)rGrD)rGrB)rGrE)rGr )rGr+)rGr�)rGr")r�r�rGrH)r�rCr�r.rGrHr�)r�r�rGr�)ry)r�r"rGrH)rGr)rGr�)r6rBrGr)r6rBrGr&)r r+r�rCr�r�rGrH) r r+r�rCr�r�r+rCrGrH)r�rr�r.rGrH)r�rErGrH)r�rKrGrH)r�r�rGrH)6�__name__� __module__� __qualname__�__doc__�__annotations__r/rxr�r�r��propertyr:r;r<r�r�r�r�r�r�r�r�r�r�NORMAL_CLOSUREr�r�r�r�r�r�r�r�rrr'r)r!r�r�r�r�r�r8r7r�r�rDrJrNrPrRrUrW�ryrwr)r)6s��������_�_�H�O�O�O��D����� %)�&(�%'�&*�$� $�� ���"�!�15� $�#O7�O7�O7�O7�O7�O7�d � � � �#�#�#�#� T�T�T�T�&�����X�� �����X�� �����X�� �8�8�8��X�8�&�8�8�8��X�8�&� O� O� O��X� O��*�*�*��X�*��(�(�(��X�(�"�*�*�*��X�*�"����"L�L�L�L�\]I�]I�]I�]I�B�,��59�59�59�59�59�n :� :� :� :�9+�9+�9+�9+�9+�v4�4�4�4�4�6����. I� I� I� I�DD;�D;�D;�D;�LG5�G5�G5�G5�R=�=�=�=�~ � � � � � � � � %� %� %� %� EJ�J� � � � � � �O�O�O�O�O�2+F�+F�+F�+F�Z4)�4)�4)�4)�l.�.�.�.�:2�2�2�2�*�.��<X�<X�<X�<X�<X�|!�!�!�!�(-�-�-�-�*$*�$*�$*�$*�L����(�(�(�(�$�$�$�$� � � � � � ryF� websockets�!Iterable[WebSocketCommonProtocol]r�r"�raise_exceptionsr+rGrHc �B�t|ttttf��st d���|r)t jdd�dkrtd���g}t|��\}}|D�]}|j tj ur�|j �B|r%td��}|�|��n|j�d���` |�d||���y#t&$r�}|r,td ��}||_|�|��nS|j�d t+jt/|��|��d �����Yd}~�� d}~wwxYw|r|rt3d |���dSdS) a Broadcast a message to several WebSocket connections. A string (:class:`str`) is sent as a Text_ frame. A bytestring or bytes-like object (:class:`bytes`, :class:`bytearray`, or :class:`memoryview`) is sent as a Binary_ frame. .. _Text: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6 .. _Binary: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6 :func:`broadcast` pushes the message synchronously to all connections even if their write buffers are overflowing. There's no backpressure. If you broadcast messages faster than a connection can handle them, messages will pile up in its write buffer until the connection times out. Keep ``ping_interval`` and ``ping_timeout`` low to prevent excessive memory usage from slow connections. Unlike :meth:`~websockets.legacy.protocol.WebSocketCommonProtocol.send`, :func:`broadcast` doesn't support sending fragmented messages. Indeed, fragmentation is useful for sending large messages without buffering them in memory, while :func:`broadcast` buffers one copy per connection as fast as possible. :func:`broadcast` skips connections that aren't open in order to avoid errors on connections where the closing handshake is in progress. :func:`broadcast` ignores failures to write the message on some connections. It continues writing to other connections. On Python 3.11 and above, you may set ``raise_exceptions`` to :obj:`True` to record failures and raise all exceptions in a :pep:`654` :exc:`ExceptionGroup`. While :func:`broadcast` makes more sense for servers, it works identically with clients, if you have a use case for opening connections to many servers and broadcasting a message to them. Args: websockets: WebSocket connections to which the message will be sent. message: Message to send. raise_exceptions: Whether to raise an exception in case of failures. Raises: TypeError: If ``message`` doesn't have a supported type. zdata must be str or bytes-likeNr )�� z.raise_exceptions requires at least Python 3.11zsending a fragmented messagez/skipped broadcast: sending a fragmented messageTzfailed to write messagez.skipped broadcast: failed to write message: %srzskipped broadcast)r�r.r�r�r�r��sys� version_info� ValueErrorr(rer!r�rrr�r�r2�warningr'r�r�� traceback�format_exception_onlyr��strip�ExceptionGroup) r`r�rb� exceptionsr�r�rKr��write_exceptions rw� broadcastrp s���d �g��U�I�z�B� C� C�:��8�9�9�9��� � �B�Q�B� �'� )� )��M�N�N� N�� ���(�(�L�F�D���� � �?�%�*� ,� ,� � � /� ;�� �(�)G�H�H� ��!�!�)�,�,�,�,�� �(�(�E���� � � � &� &�t�V�T� :� :� :� :��� � � �� �(�)B�C�C� �&5� �#��!�!�)�,�,�,�,�� �(�(�D��3��_�-�-�'���� ����� ������������ �����>�J�>��0�*�=�=�=�>�>�>�>s�C0�0 F�:BF�Fzwebsockets.legacy.server)F)r`rar�r"rbr+rGrH)?� __future__rrPrrmrUr�rr�rfr�rjrRrM�collections.abcrrrrr�typingr r r r �asyncio.compatibilityr�datastructuresrrnrrrrrrrfr�framesrrrrrrrrrr �protocolr!r"r#r$�framingr&r'r(�__all__�Protocolr)rprYr_ryrw�<module>r{s���"�"�"�"�"�"����� � � � ��������� � � � � � � � � � � � � � � � � � � � ����� � � � �����V�V�V�V�V�V�V�V�V�V�V�V�V�V�-�-�-�-�-�-�-�-�-�-�-�-�3�3�3�3�3�3�$�$�$�$�$�$�����������������#�"�"�"�"�"� � � � � � � � � � � � � � � � � � � � � � � � �������2�2�2�2�2�2�2�2�2�2�6�6�6�6�6�6�6�6�6�6� %� %��J�J�J�J�J�g�.�J�J�J�l.#�\>�\>�\>�\>�\>�@2� ���ry
Memory