o
    ¨Ýj8#  ã                   @   s:   d dl Z d dlmZ d dlmZmZmZ G dd„ dƒZdS )é    N)ÚAsyncClient)ÚSocketIOErrorÚTimeoutErrorÚDisconnectedErrorc                   @   s€   e Zd ZdZeZdd„ Zi dddddfdd	„Zed
d„ ƒZ	edd„ ƒZ
ddd„Zddd„Zddd„Zdd„ Zdd„ Zdd„ ZdS )ÚAsyncSimpleClienta+  A Socket.IO client.

    This class implements a simple, yet fully compliant Socket.IO web client
    with support for websocket and long-polling transports.

    The positional and keyword arguments given in the constructor are passed
    to the underlying :func:`socketio.AsyncClient` object.
    c                 O   s<   || _ || _d | _d| _t ¡ | _d| _t ¡ | _g | _	d S )Nú/F)
Úclient_argsÚclient_kwargsÚclientÚ	namespaceÚasyncioÚEventÚconnected_eventÚ	connectedÚinput_eventÚinput_buffer)ÚselfÚargsÚkwargs© r   úW/var/www/html/chatgpt/venv/lib/python3.10/site-packages/socketio/async_simple_client.pyÚ__init__   s   


zAsyncSimpleClient.__init__Nr   z	socket.ioé   c              	   ƒ   sÔ   �ˆ j rtdƒ‚|ˆ _g ˆ _ˆ j ¡  ˆ jˆ ji ˆ j¤Žˆ _	ˆ j	j
ˆ jd�‡ fdd„ƒ}ˆ j	j
ˆ jd�‡ fdd„ƒ}	ˆ j	j
ˆ jd�‡ fdd„ƒ}
ˆ j	jd	ˆ jd�‡ fd
d„ƒ}ˆ j	j|||||g||d�I dH  dS )aH  Connect to a Socket.IO server.

        :param url: The URL of the Socket.IO server. It can include custom
                    query string parameters if required by the server. If a
                    function is provided, the client will invoke it to obtain
                    the URL each time a connection or reconnection is
                    attempted.
        :param headers: A dictionary with custom headers to send with the
                        connection request. If a function is provided, the
                        client will invoke it to obtain the headers dictionary
                        each time a connection or reconnection is attempted.
        :param auth: Authentication data passed to the server with the
                     connection request, normally a dictionary with one or
                     more string key/value pairs. If a function is provided,
                     the client will invoke it to obtain the authentication
                     data each time a connection or reconnection is attempted.
        :param transports: The list of allowed transports. Valid transports
                           are ``'polling'`` and ``'websocket'``. If not
                           given, the polling transport is connected first,
                           then an upgrade to websocket is attempted.
        :param namespace: The namespace to connect to as a string. If not
                          given, the default namespace ``/`` is used.
        :param socketio_path: The endpoint where the Socket.IO server is
                              installed. The default value is appropriate for
                              most cases.
        :param wait_timeout: How long the client should wait for the
                             connection. The default is 5 seconds.

        Note: this method is a coroutine.
        zAlready connected©r   c                      ó   dˆ _ ˆ j ¡  d S )NT©r   r   Úsetr   ©r   r   r   ÚconnectD   ó   z*AsyncSimpleClient.connect.<locals>.connectc                      s   ˆ j  ¡  d S ©N)r   Úclearr   r   r   r   Ú
disconnectI   s   z-AsyncSimpleClient.connect.<locals>.disconnectc                      r   )NFr   r   r   r   r   Z__disconnect_finalM   r   z5AsyncSimpleClient.connect.<locals>.__disconnect_finalÚ*c                    s    ˆ j  | g|¢¡ ˆ j ¡  d S r    )r   Úappendr   r   )Úeventr   r   r   r   Úon_eventR   s   z+AsyncSimpleClient.connect.<locals>.on_event)ÚheadersÚauthÚ
transportsÚ
namespacesÚsocketio_pathÚwait_timeoutN)r   ÚRuntimeErrorr   r   r   r!   Úclient_classr   r	   r
   r%   Úonr   )r   Úurlr'   r(   r)   r   r+   r,   r   r"   Z$_AsyncSimpleClient__disconnect_finalr&   r   r   r   r      s0   €!
ÿÿýzAsyncSimpleClient.connectc                 C   s   | j r
| j  | j¡S dS )zÆThe session ID received from the server.

        The session ID is not guaranteed to remain constant throughout the life
        of the connection, as reconnections can cause it to change.
        N)r
   Zget_sidr   r   r   r   r   Úsid\   s   zAsyncSimpleClient.sidc                 C   s   | j r| j  ¡ S dS )zœThe name of the transport currently in use.

        The transport is returned as a string and can be one of ``polling``
        and ``websocket``.
        Ú )r
   Ú	transportr   r   r   r   r3   e   s   zAsyncSimpleClient.transportc                 Ã   sR   �	 | j  ¡ I dH  | jstƒ ‚z| jj||| jd�I dH W S  ty'   Y nw q)a?  Emit an event to the server.

        :param event: The event name. It can be any string. The event names
                      ``'connect'``, ``'message'`` and ``'disconnect'`` are
                      reserved and should not be used.
        :param data: The data to send to the server. Data can be of
                     type ``str``, ``bytes``, ``list`` or ``dict``. To send
                     multiple arguments, use a tuple where each element is of
                     one of the types indicated above.

        Note: this method is a coroutine.

        This method schedules the event to be sent out and returns, without
        actually waiting for its delivery. In cases where the client needs to
        ensure that the event was received, :func:`socketio.SimpleClient.call`
        should be used instead.
        TNr   )r   Úwaitr   r   r
   Úemitr   r   )r   r%   Údatar   r   r   r5   n   s   €
ÿÿùzAsyncSimpleClient.emité<   c                 Ã   sb   �	 | j  ¡ I dH  | jstƒ ‚z| jj||| j|d�I dH W S  ty'   ‚  ty/   Y nw q)aó  Emit an event to the server and wait for a response.

        This method issues an emit and waits for the server to provide a
        response or acknowledgement. If the response does not arrive before the
        timeout, then a ``TimeoutError`` exception is raised.

        :param event: The event name. It can be any string. The event names
                      ``'connect'``, ``'message'`` and ``'disconnect'`` are
                      reserved and should not be used.
        :param data: The data to send to the server. Data can be of
                     type ``str``, ``bytes``, ``list`` or ``dict``. To send
                     multiple arguments, use a tuple where each element is of
                     one of the types indicated above.
        :param timeout: The waiting timeout. If the timeout is reached before
                        the server acknowledges the event, then a
                        ``TimeoutError`` exception is raised.

        Note: this method is a coroutine.
        TN)r   Útimeout)	r   r4   r   r   r
   Úcallr   r   r   )r   r%   r6   r8   r   r   r   r9   Š   s    €
þÿözAsyncSimpleClient.callc                 Ã   sœ   �| j sHztj| j ¡ |d�I dH  W n tjy   tƒ ‚w | js%tƒ ‚ztj| j ¡ |d�I dH  W n tjy?   tƒ ‚w | j 	¡  | j r| j  
d¡S )aã  Wait for an event from the server.

        :param timeout: The waiting timeout. If the timeout is reached before
                        the server acknowledges the event, then a
                        ``TimeoutError`` exception is raised.

        Note: this method is a coroutine.

        The return value is a list with the event name as the first element. If
        the server included arguments with the event, they are returned as
        additional list elements.
        )r8   Nr   )r   r   Úwait_forr   r4   r   r   r   r   r!   Úpop)r   r8   r   r   r   Úreceive«   s*   €ÿÿÿÿ
ózAsyncSimpleClient.receivec                 Ã   s,   �| j r| j ¡ I dH  d| _d| _ dS dS )zODisconnect from the server.

        Note: this method is a coroutine.
        NF)r   r
   r"   r   r   r   r   r"   È   s   €
ýzAsyncSimpleClient.disconnectc                 Ã   s   �| S r    r   r   r   r   r   Ú
__aenter__Ò   s   €zAsyncSimpleClient.__aenter__c                 Ã   s   �|   ¡ I d H  d S r    )r"   )r   Úexc_typeÚexc_valÚexc_tbr   r   r   Ú	__aexit__Õ   s   €zAsyncSimpleClient.__aexit__r    )Nr7   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r.   r   r   Úpropertyr1   r3   r5   r9   r<   r"   r=   rA   r   r   r   r   r      s$    

þA




!
r   )r   Zsocketior   Zsocketio.exceptionsr   r   r   r   r   r   r   r   Ú<module>   s    