o
    ¨Ýj�)  ã                   @   sP   d dl Z d dlZddlmZ ddlmZ ddlmZ e  d¡ZG dd„ dƒZdS )	é    Né   )Úmanager)Úbase_namespace)Úpacketzsocketio.serverc                   @   sœ   e Zd ZddgZejjZ			d#dd„Zd	d
„ Zd$dd„Z	dd„ Z
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d„ Zdd „ Zd!d"„ ZdS )&Ú
BaseServerÚconnectÚ
disconnectNFÚdefaultTc                 K   s€  |}	|	  dd ¡}
|
d ur|
|	d< |dkrtj| _n|dkr(ddlm} |j| _n|| _|d ur7|| j_||	d< d|	d	< |  ¡ di |	¤Ž| _	| j	 
d
| j¡ | j	 
d| j¡ | j	 
d| j¡ i | _i | _i | _tƒ | _i | _t|tƒsv|| _n#t| _| jjtjkr™|rŠ| j tj¡ n| j tj¡ | j t ¡ ¡ |d u r¡t  !¡ }|| _ | j  "| ¡ d| _#|| _$|| _%|p·dg| _&| j	j'| _'d S )NÚengineio_loggerÚloggerr	   Úmsgpackr   )Úmsgpack_packetÚjsonFÚasync_handlersr   Úmessager   ú/© )(Úpopr   ZPacketZpacket_classÚ r   ZMsgPackPacketr   Ú_engineio_server_classÚeioÚonÚ_handle_eio_connectÚ_handle_eio_messageÚ_handle_eio_disconnectÚenvironÚhandlersÚnamespace_handlersÚobjectZnot_handledZ_binary_packetÚ
isinstanceÚboolr   Údefault_loggerÚlevelÚloggingÚNOTSETÚsetLevelÚINFOÚERRORÚ
addHandlerÚStreamHandlerr   ÚManagerZ
set_serverZmanager_initializedr   Úalways_connectÚ
namespacesZ
async_mode)ÚselfZclient_managerr   Ú
serializerr   r   r+   r,   ÚkwargsZengineio_optionsr
   r   r   r   úO/var/www/html/chatgpt/venv/lib/python3.10/site-packages/socketio/base_server.pyÚ__init__   sP   


zBaseServer.__init__c                 C   s   dS )NFr   ©r-   r   r   r0   Úis_asyncio_basedE   s   zBaseServer.is_asyncio_basedc                    s0   ˆpd‰‡ ‡‡fdd„}|du r|S ||ƒ dS )aã	  Register an event handler.

        :param event: The event name. It can be any string. The event names
                      ``'connect'``, ``'message'`` and ``'disconnect'`` are
                      reserved and should not be used. The ``'*'`` event name
                      can be used to define a catch-all event handler.
        :param handler: The function that should be invoked to handle the
                        event. When this parameter is not given, the method
                        acts as a decorator for the handler function.
        :param namespace: The Socket.IO namespace for the event. If this
                          argument is omitted the handler is associated with
                          the default namespace. A catch-all namespace can be
                          defined by passing ``'*'`` as the namespace.

        Example usage::

            # as a decorator:
            @sio.on('connect', namespace='/chat')
            def connect_handler(sid, environ):
                print('Connection request')
                if environ['REMOTE_ADDR'] in blacklisted:
                    return False  # reject

            # as a method:
            def message_handler(sid, msg):
                print('Received message: ', msg)
                sio.send(sid, 'response')
            socket_io.on('message', namespace='/chat', handler=message_handler)

        The arguments passed to the handler function depend on the event type:

        - The ``'connect'`` event handler receives the ``sid`` (session ID) for
          the client and the WSGI environment dictionary as arguments.
        - The ``'disconnect'`` handler receives the ``sid`` for the client as
          only argument.
        - The ``'message'`` handler and handlers for custom event names receive
          the ``sid`` for the client and the message payload as arguments. Any
          values returned from a message handler will be passed to the client's
          acknowledgement callback function if it exists.
        - A catch-all event handler receives the event name as first argument,
          followed by any arguments specific to the event.
        - A catch-all namespace event handler receives the namespace as first
          argument, followed by any arguments specific to the event.
        - A combined catch-all namespace and catch-all event handler receives
          the event name as first argument and the namespace as second
          argument, followed by any arguments specific to the event.
        r   c                    s&   ˆˆj vr
i ˆj ˆ< | ˆj ˆ ˆ < | S ©N)r   ©Úhandler©ÚeventÚ	namespacer-   r   r0   Úset_handlerz   s   

z"BaseServer.on.<locals>.set_handlerNr   )r-   r8   r6   r9   r:   r   r7   r0   r   H   s
   0zBaseServer.onc                    sP   t ˆ ƒdkrt ˆƒdkrtˆ d ƒrˆ ˆ d j¡ˆ d ƒS ‡ ‡‡fdd„}|S )a�  Decorator to register an event handler.

        This is a simplified version of the ``on()`` method that takes the
        event name from the decorated function.

        Example usage::

            @sio.event
            def my_event(data):
                print('Received data: ', data)

        The above example is equivalent to::

            @sio.on('my_event')
            def my_event(data):
                print('Received data: ', data)

        A custom namespace can be given as an argument to the decorator::

            @sio.event(namespace='/test')
            def my_event(data):
                print('Received data: ', data)
        r   r   c                    s   ˆj | jgˆ ¢R i ˆ¤Ž| ƒS r4   )r   Ú__name__r5   ©Úargsr/   r-   r   r0   r:   ¢   s   z%BaseServer.event.<locals>.set_handler)ÚlenÚcallabler   r;   )r-   r=   r/   r:   r   r<   r0   r8   „   s   $zBaseServer.eventc                 C   sF   t |tjƒs
tdƒ‚|  ¡ | ¡ krtdƒ‚| | ¡ || j|j< dS )zõRegister a namespace handler object.

        :param namespace_handler: An instance of a :class:`Namespace`
                                  subclass that handles all the event traffic
                                  for a namespace.
        zNot a namespace instancez+Not a valid namespace class for this serverN)r   r   ZBaseServerNamespaceÚ
ValueErrorr3   Z_set_serverr   r9   )r-   Únamespace_handlerr   r   r0   Úregister_namespace§   s   ÿ
ÿzBaseServer.register_namespacec                 C   s   |pd}| j  ||¡S )zîReturn the rooms a client is in.

        :param sid: Session ID of the client.
        :param namespace: The Socket.IO namespace for the event. If this
                          argument is omitted the default namespace is used.
        r   )r   Z	get_rooms)r-   Úsidr9   r   r   r0   Úrooms·   s   zBaseServer.roomsc                 C   ó   | j  ||pd¡}| j |¡S )a_  Return the name of the transport used by the client.

        The two possible values returned by this function are ``'polling'``
        and ``'websocket'``.

        :param sid: The session of the client.
        :param namespace: The Socket.IO namespace. If this argument is omitted
                          the default namespace is used.
        r   )r   Úeio_sid_from_sidr   Ú	transport©r-   rC   r9   Zeio_sidr   r   r0   rG   Á   s   
zBaseServer.transportc                 C   rE   )zñReturn the WSGI environ dictionary for a client.

        :param sid: The session of the client.
        :param namespace: The Socket.IO namespace. If this argument is omitted
                          the default namespace is used.
        r   )r   rF   r   ÚgetrH   r   r   r0   Úget_environÎ   s   zBaseServer.get_environc                 C   sÜ   d }|| j v r/|| j | v r| j | | }n|| jvr/d| j | v r/| j | d }|g|¢R }|d u rjd| j v rj|| j d v rP| j d | }|g|¢R }||fS || jvrjd| j d v rj| j d d }||g|¢R }||fS ©NÚ*)r   Úreserved_events)r-   r8   r9   r=   r6   r   r   r0   Ú_get_event_handlerØ   s$   


üzBaseServer._get_event_handlerc                 C   sH   d }|| j v r| j | }|d u r d| j v r | j d }|g|¢R }||fS rK   )r   )r-   r9   r=   r6   r   r   r0   Ú_get_namespace_handlerò   s   


z!BaseServer._get_namespace_handlerc                 C   ó   t ƒ ‚r4   ©ÚNotImplementedErrorr2   r   r   r0   r      ó   zBaseServer._handle_eio_connectc                 C   rP   r4   rQ   )r-   Údatar   r   r0   r     rS   zBaseServer._handle_eio_messagec                 C   rP   r4   rQ   r2   r   r   r0   r     rS   z!BaseServer._handle_eio_disconnectc                 C   s   t dƒ‚)Nz!Must be implemented in subclassesrQ   r2   r   r   r0   r   	  s   z!BaseServer._engineio_server_class)NFr	   NTFN)NNr4   )r;   Ú
__module__Ú__qualname__rM   ÚengineioÚServerÚreasonr1   r3   r   r8   rB   rD   rG   rJ   rN   rO   r   r   r   r   r   r   r   r0   r      s(    
þ5
<#




r   )	r#   rW   r   r   r   r   Ú	getLoggerr!   r   r   r   r   r0   Ú<module>   s    
