
    wg"                     @    d dl Z d dlmZ d dlmZmZmZ  G d d      Zy)    N)AsyncClient)SocketIOErrorTimeoutErrorDisconnectedErrorc                   t    e Zd ZdZd Zi dddddfdZed        Zed	        Zdd
Z	ddZ
ddZd Zd Zd Zy)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                     || _         || _        d | _        d| _        t	        j
                         | _        d| _        t	        j
                         | _        g | _	        y )N/F)
client_argsclient_kwargsclient	namespaceasyncioEventconnected_event	connectedinput_eventinput_buffer)selfargskwargss      a/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/socketio/async_simple_client.py__init__zAsyncSimpleClient.__init__   sK    #&}}"==?    Nr
   z	socket.io   c           	         K    j                   rt        d      | _        g  _         j                  j                          t         j                  i  j                   _	         j                  j                   j                         fd       } j                  j                   j                         fd       }	 j                  j                   j                         fd       }
 j                  j                  d j                         fd       } j                  j                  |||||g||       d	{    y	7 w)
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                  H    d _          j                  j                          y )NTr   r   setr   s   r   connectz*AsyncSimpleClient.connect.<locals>.connectA   s    !DN  $$&r   c                  :     j                   j                          y N)r   clearr!   s   r   
disconnectz-AsyncSimpleClient.connect.<locals>.disconnectF   s      &&(r   c                  H    d _          j                  j                          y )NFr   r!   s   r   __disconnect_finalz5AsyncSimpleClient.connect.<locals>.__disconnect_finalJ   s    "DN  $$&r   *c                 v    j                   j                  | g|       j                  j                          y r$   )r   appendr   r    )eventr   r   s     r   on_eventz+AsyncSimpleClient.connect.<locals>.on_eventO   s/    $$e^d^4  "r   )headersauth
transports
namespacessocketio_pathwait_timeoutN)r   RuntimeErrorr   r   r   r%   r   r   r   r   r,   onr"   )r   urlr.   r/   r0   r   r2   r3   r"   r&   $_AsyncSimpleClient__disconnect_finalr-   s   `           r   r"   zAsyncSimpleClient.connect   s6    B >>233" !4#3#3Jt7I7IJ			T^^		4	' 
5	' 
		T^^		4	) 
5	) 
		T^^		4	' 
5	' 
t~~	6	# 
7	# kk!!t
!{-% " ' 	' 	's   EEEEc                 h    | j                   r%| j                   j                  | j                        S dS )zThe 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   get_sidr   r!   s    r   sidzAsyncSimpleClient.sidY   s(     7;kkt{{""4>>2KtKr   c                 J    | j                   r| j                   j                  S dS )zThe name of the transport currently in use.

        The transport is returned as a string and can be one of ``polling``
        and ``websocket``.
         )r   	transportr!   s    r   r=   zAsyncSimpleClient.transportb   s     )-t{{$$;;r   c                   K   	 | j                   j                          d{    | j                  s
t               	 | j                  j                  ||| j                         d{   S 7 K7 # t        $ r Y nw xY w}w)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.
        Nr   )r   waitr   r   r   emitr   r   )r   r,   datas      r   r@   zAsyncSimpleClient.emitk   s     $ &&++--->>'))![[--eT8< . H H H	 .H   sD   BA-B+A1 (A/)A1 ,B/A1 1	A=:B<A==Bc                   K   	 | j                   j                          d{    | j                  s
t               	 | j                  j                  ||| j                  |       d{   S 7 L7 # t        $ r Y nw xY w~w)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.
        N)r   timeout)r   r?   r   r   r   callr   r   )r   r,   rA   rC   s       r   rD   zAsyncSimpleClient.call   s     ( &&++--->>'))![[--eT8<6= . ? ? ?	 .? !  sD   BA.B,A2 )A0*A2 -B0A2 2	A>;B=A>>Bc                 >  K   | j                   s	 t        j                  | j                  j	                         |       d{    | j                  s
t               	 t        j                  | j                  j	                         |       d{    | j                  j                          | j                   s| j                   j                  d      S 7 # t        j
                  $ r t               w xY w7 i# t        j
                  $ r t               w xY ww)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.
        )rC   Nr   )r   r   wait_forr   r?   r   r   r   r   r%   pop)r   rC   s     r   receivezAsyncSimpleClient.receive   s      ##%&&t';';'@'@'B/68 8 8 >>'))%&&t'7'7'<'<'>/68 8 8 ""$ ##   $$Q''8'' %"n$%
8'' %"n$%s^   D2C CC D2C< C:C< &D<DC C77D:C< <DDc                    K   | j                   r1| j                  j                          d{    d| _        d| _         yy7 w)zODisconnect from the server.

        Note: this method is a coroutine.
        NF)r   r   r&   r!   s    r   r&   zAsyncSimpleClient.disconnect   s;     
 >>++((***DK"DN *s   *AAAc                    K   | S wr$    r!   s    r   
__aenter__zAsyncSimpleClient.__aenter__   s     s   c                 @   K   | j                          d {    y 7 wr$   )r&   )r   exc_typeexc_valexc_tbs       r   	__aexit__zAsyncSimpleClient.__aexit__   s     oos   r$   )N<   )__name__
__module____qualname____doc__r   r"   propertyr:   r=   r@   rD   rH   r&   rL   rQ   rK   r   r   r   r      sk     *,$4 #;#$>'@ L L < <8>(:# r   r   )r   socketior   socketio.exceptionsr   r   r   r   rK   r   r   <module>rZ      s       N NK  K r   