o
    ¨Ýjˆ  ã                   @   s(   d dl Z ddlmZ G dd„ deƒZdS )é    Né   )ÚPubSubManagerc                       sB   e Zd ZdZdZ		d‡ fdd„	Zd	d
„ Zdd„ Zdd„ Z‡  Z	S )Ú
ZmqManageraã  zmq based client manager.

    NOTE: this zmq implementation should be considered experimental at this
    time. At this time, eventlet is required to use zmq.

    This class implements a zmq backend for event sharing across multiple
    processes. To use a zmq backend, initialize the :class:`Server` instance as
    follows::

        url = 'zmq+tcp://hostname:port1+port2'
        server = socketio.Server(client_manager=socketio.ZmqManager(url))

    :param url: The connection URL for the zmq message broker,
                which will need to be provided and running.
    :param channel: The channel name on which the server sends and receives
                    notifications. Must be the same in all the servers.
    :param write_only: If set to ``True``, only initialize to emit events. The
                       default of ``False`` initializes the class for emitting
                       and receiving. A write-only instance can be used
                       independently of the server to emit to clients from an
                       external process.
    :param logger: a custom logger to log it. If not given, the server logger
                   is used.
    :param json: An alternative JSON module to use for encoding and decoding
                 packets. Custom json modules must have ``dumps`` and ``loads``
                 functions that are compatible with the standard library
                 versions. This setting is only used when ``write_only`` is set
                 to ``True``. Otherwise the JSON module configured in the
                 server is used.

    A zmq message broker must be running for the zmq_manager to work.
    you can write your own or adapt one from the following simple broker
    below::

        import zmq

        receiver = zmq.Context().socket(zmq.PULL)
        receiver.bind("tcp://*:5555")

        publisher = zmq.Context().socket(zmq.PUB)
        publisher.bind("tcp://*:5556")

        while True:
            publisher.send(receiver.recv())
    Úzmqúzmq+tcp://localhost:5555+5556ÚsocketioFNc                    sò   zddl m} W n ty   tdƒ‚w t d¡}| d¡r#| |¡s)td| ƒ‚tƒ j	||||d� | 
dd	¡}| d
¡\}}	| d¡d }
| 
|
|	¡}| ¡  |j¡}| |¡ | ¡  |j¡}| |jd	¡ | |¡ || _|| _|| _d S )Nr   )r   zJzmq package is not installed (Run "pip install pyzmq" in your virtualenv).z
:\d+\+\d+$z
zmq+tcp://zunexpected connection string: )ÚchannelÚ
write_onlyÚloggerÚjsonzzmq+Ú ú+ú:éÿÿÿÿ)Zeventlet.greenr   ÚImportErrorÚRuntimeErrorÚreÚcompileÚ
startswithÚsearchÚsuperÚ__init__ÚreplaceÚsplitÚContextÚsocketZPUSHÚconnectZSUBZsetsockopt_stringZ	SUBSCRIBEÚsinkÚsubr   )ÚselfÚurlr   r	   r
   r   r   ÚrZsink_urlZsub_portZ	sink_portZsub_urlr   r   ©Ú	__class__© úO/var/www/html/chatgpt/venv/lib/python3.10/site-packages/socketio/zmq_manager.pyr   6   s.   ÿ
ÿ


zZmqManager.__init__c                 C   s&   | j  d| j|dœ¡ ¡ }| j |¡S )NÚmessage)Útyper   Údata)r   Údumpsr   Úencoder   Úsend)r   r(   Zpacked_datar$   r$   r%   Ú_publishU   s   ýÿúzZmqManager._publishc                 c   s   � 	 | j  ¡ }|d ur|V  q)N)r   Úrecv)r   Úresponser$   r$   r%   Ú
zmq_listen_   s   €
ýzZmqManager.zmq_listenc              	   c   sx   � |   ¡ D ]4}t|tƒrz| j |¡}W n	 ty   Y nw t|tƒr9|d dkr9|d | jkr9d|v r9|d V  qd S )Nr'   r&   r   r(   )r/   Ú
isinstanceÚbytesr   ÚloadsÚ	ExceptionÚdictr   )r   r&   r$   r$   r%   Ú_listene   s   €
ÿ

€zZmqManager._listen)r   r   FNN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Únamer   r,   r/   r5   Ú__classcell__r$   r$   r"   r%   r      s    -ÿ
r   )r   Zpubsub_managerr   r   r$   r$   r$   r%   Ú<module>   s    