2020-01-30 01:07:26 +00:00
|
|
|
import itertools
|
|
|
|
import logging
|
|
|
|
import random
|
|
|
|
import signal
|
2021-05-08 14:25:29 +00:00
|
|
|
import threading
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
import engineio
|
|
|
|
|
|
|
|
from . import exceptions
|
|
|
|
from . import namespace
|
|
|
|
from . import packet
|
|
|
|
|
|
|
|
default_logger = logging.getLogger('socketio.client')
|
|
|
|
reconnecting_clients = []
|
|
|
|
|
|
|
|
|
|
|
|
def signal_handler(sig, frame): # pragma: no cover
|
|
|
|
"""SIGINT handler.
|
|
|
|
|
|
|
|
Notify any clients that are in a reconnect loop to abort. Other
|
|
|
|
disconnection tasks are handled at the engine.io level.
|
|
|
|
"""
|
|
|
|
for client in reconnecting_clients[:]:
|
|
|
|
client._reconnect_abort.set()
|
2021-05-08 14:25:29 +00:00
|
|
|
if callable(original_signal_handler):
|
|
|
|
return original_signal_handler(sig, frame)
|
|
|
|
else: # pragma: no cover
|
|
|
|
# Handle case where no original SIGINT handler was present.
|
|
|
|
return signal.default_int_handler(sig, frame)
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
|
2021-05-08 14:25:29 +00:00
|
|
|
original_signal_handler = None
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Client(object):
|
|
|
|
"""A Socket.IO client.
|
|
|
|
|
|
|
|
This class implements a fully compliant Socket.IO web client with support
|
|
|
|
for websocket and long-polling transports.
|
|
|
|
|
|
|
|
:param reconnection: ``True`` if the client should automatically attempt to
|
|
|
|
reconnect to the server after an interruption, or
|
|
|
|
``False`` to not reconnect. The default is ``True``.
|
|
|
|
:param reconnection_attempts: How many reconnection attempts to issue
|
2022-01-24 04:07:52 +00:00
|
|
|
before giving up, or 0 for infinite attempts.
|
2020-01-30 01:07:26 +00:00
|
|
|
The default is 0.
|
|
|
|
:param reconnection_delay: How long to wait in seconds before the first
|
|
|
|
reconnection attempt. Each successive attempt
|
|
|
|
doubles this delay.
|
|
|
|
:param reconnection_delay_max: The maximum delay between reconnection
|
|
|
|
attempts.
|
|
|
|
:param randomization_factor: Randomization amount for each delay between
|
|
|
|
reconnection attempts. The default is 0.5,
|
|
|
|
which means that each delay is randomly
|
|
|
|
adjusted by +/- 50%.
|
|
|
|
:param logger: To enable logging set to ``True`` or pass a logger object to
|
|
|
|
use. To disable logging set to ``False``. The default is
|
2021-05-08 14:25:29 +00:00
|
|
|
``False``. Note that fatal errors are logged even when
|
|
|
|
``logger`` is ``False``.
|
2022-01-24 04:07:52 +00:00
|
|
|
:param serializer: The serialization method to use when transmitting
|
|
|
|
packets. Valid values are ``'default'``, ``'pickle'``,
|
|
|
|
``'msgpack'`` and ``'cbor'``. Alternatively, a subclass
|
|
|
|
of the :class:`Packet` class with custom implementations
|
|
|
|
of the ``encode()`` and ``decode()`` methods can be
|
|
|
|
provided. Client and server must use compatible
|
|
|
|
serializers.
|
2020-01-30 01:07:26 +00:00
|
|
|
: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.
|
2022-01-24 04:07:52 +00:00
|
|
|
:param handle_sigint: Set to ``True`` to automatically handle disconnection
|
|
|
|
when the process is interrupted, or to ``False`` to
|
|
|
|
leave interrupt handling to the calling application.
|
|
|
|
Interrupt handling can only be enabled when the
|
|
|
|
client instance is created in the main thread.
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
The Engine.IO configuration supports the following settings:
|
|
|
|
|
|
|
|
:param request_timeout: A timeout in seconds for requests. The default is
|
|
|
|
5 seconds.
|
2021-05-08 14:25:29 +00:00
|
|
|
:param http_session: an initialized ``requests.Session`` object to be used
|
|
|
|
when sending requests to the server. Use it if you
|
|
|
|
need to add special client options such as proxy
|
|
|
|
servers, SSL certificates, etc.
|
2020-01-30 01:07:26 +00:00
|
|
|
:param ssl_verify: ``True`` to verify SSL certificates, or ``False`` to
|
|
|
|
skip SSL certificate verification, allowing
|
|
|
|
connections to servers with self signed certificates.
|
|
|
|
The default is ``True``.
|
|
|
|
:param engineio_logger: To enable Engine.IO logging set to ``True`` or pass
|
|
|
|
a logger object to use. To disable logging set to
|
2021-05-08 14:25:29 +00:00
|
|
|
``False``. The default is ``False``. Note that
|
|
|
|
fatal errors are logged even when
|
|
|
|
``engineio_logger`` is ``False``.
|
2020-01-30 01:07:26 +00:00
|
|
|
"""
|
2022-01-24 04:07:52 +00:00
|
|
|
reserved_events = ['connect', 'connect_error', 'disconnect']
|
|
|
|
|
2020-01-30 01:07:26 +00:00
|
|
|
def __init__(self, reconnection=True, reconnection_attempts=0,
|
|
|
|
reconnection_delay=1, reconnection_delay_max=5,
|
2022-01-24 04:07:52 +00:00
|
|
|
randomization_factor=0.5, logger=False, serializer='default',
|
|
|
|
json=None, handle_sigint=True, **kwargs):
|
2021-05-08 14:25:29 +00:00
|
|
|
global original_signal_handler
|
2022-01-24 04:07:52 +00:00
|
|
|
if handle_sigint and original_signal_handler is None and \
|
2021-05-08 14:25:29 +00:00
|
|
|
threading.current_thread() == threading.main_thread():
|
|
|
|
original_signal_handler = signal.signal(signal.SIGINT,
|
|
|
|
signal_handler)
|
2020-01-30 01:07:26 +00:00
|
|
|
self.reconnection = reconnection
|
|
|
|
self.reconnection_attempts = reconnection_attempts
|
|
|
|
self.reconnection_delay = reconnection_delay
|
|
|
|
self.reconnection_delay_max = reconnection_delay_max
|
|
|
|
self.randomization_factor = randomization_factor
|
2022-01-24 04:07:52 +00:00
|
|
|
self.handle_sigint = handle_sigint
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
engineio_options = kwargs
|
2022-01-24 04:07:52 +00:00
|
|
|
engineio_options['handle_sigint'] = handle_sigint
|
2020-01-30 01:07:26 +00:00
|
|
|
engineio_logger = engineio_options.pop('engineio_logger', None)
|
|
|
|
if engineio_logger is not None:
|
|
|
|
engineio_options['logger'] = engineio_logger
|
2022-01-24 04:07:52 +00:00
|
|
|
if serializer == 'default':
|
|
|
|
self.packet_class = packet.Packet
|
|
|
|
elif serializer == 'msgpack':
|
|
|
|
from . import msgpack_packet
|
|
|
|
self.packet_class = msgpack_packet.MsgPackPacket
|
|
|
|
else:
|
|
|
|
self.packet_class = serializer
|
2020-01-30 01:07:26 +00:00
|
|
|
if json is not None:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.packet_class.json = json
|
2020-01-30 01:07:26 +00:00
|
|
|
engineio_options['json'] = json
|
|
|
|
|
|
|
|
self.eio = self._engineio_client_class()(**engineio_options)
|
|
|
|
self.eio.on('connect', self._handle_eio_connect)
|
|
|
|
self.eio.on('message', self._handle_eio_message)
|
|
|
|
self.eio.on('disconnect', self._handle_eio_disconnect)
|
|
|
|
|
|
|
|
if not isinstance(logger, bool):
|
|
|
|
self.logger = logger
|
|
|
|
else:
|
|
|
|
self.logger = default_logger
|
2021-05-08 14:25:29 +00:00
|
|
|
if self.logger.level == logging.NOTSET:
|
2020-01-30 01:07:26 +00:00
|
|
|
if logger:
|
|
|
|
self.logger.setLevel(logging.INFO)
|
|
|
|
else:
|
|
|
|
self.logger.setLevel(logging.ERROR)
|
|
|
|
self.logger.addHandler(logging.StreamHandler())
|
|
|
|
|
|
|
|
self.connection_url = None
|
|
|
|
self.connection_headers = None
|
2022-01-24 04:07:52 +00:00
|
|
|
self.connection_auth = None
|
2020-01-30 01:07:26 +00:00
|
|
|
self.connection_transports = None
|
2021-05-08 14:25:29 +00:00
|
|
|
self.connection_namespaces = []
|
2020-01-30 01:07:26 +00:00
|
|
|
self.socketio_path = None
|
|
|
|
self.sid = None
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
self.connected = False #: Indicates if the client is connected or not.
|
|
|
|
self.namespaces = {} #: set of connected namespaces.
|
2020-01-30 01:07:26 +00:00
|
|
|
self.handlers = {}
|
|
|
|
self.namespace_handlers = {}
|
|
|
|
self.callbacks = {}
|
|
|
|
self._binary_packet = None
|
2021-05-08 14:25:29 +00:00
|
|
|
self._connect_event = None
|
2020-01-30 01:07:26 +00:00
|
|
|
self._reconnect_task = None
|
2021-05-08 14:25:29 +00:00
|
|
|
self._reconnect_abort = None
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
def is_asyncio_based(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def on(self, event, handler=None, namespace=None):
|
|
|
|
"""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.
|
|
|
|
: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.
|
|
|
|
|
|
|
|
Example usage::
|
|
|
|
|
|
|
|
# as a decorator:
|
|
|
|
@sio.on('connect')
|
|
|
|
def connect_handler():
|
|
|
|
print('Connected!')
|
|
|
|
|
|
|
|
# as a method:
|
|
|
|
def message_handler(msg):
|
|
|
|
print('Received message: ', msg)
|
|
|
|
sio.send( 'response')
|
|
|
|
sio.on('message', message_handler)
|
|
|
|
|
|
|
|
The ``'connect'`` event handler receives no arguments. The
|
|
|
|
``'message'`` handler and handlers for custom event names receive the
|
|
|
|
message payload as only argument. Any values returned from a message
|
|
|
|
handler will be passed to the client's acknowledgement callback
|
|
|
|
function if it exists. The ``'disconnect'`` handler does not take
|
|
|
|
arguments.
|
|
|
|
"""
|
|
|
|
namespace = namespace or '/'
|
|
|
|
|
|
|
|
def set_handler(handler):
|
|
|
|
if namespace not in self.handlers:
|
|
|
|
self.handlers[namespace] = {}
|
|
|
|
self.handlers[namespace][event] = handler
|
|
|
|
return handler
|
|
|
|
|
|
|
|
if handler is None:
|
|
|
|
return set_handler
|
|
|
|
set_handler(handler)
|
|
|
|
|
|
|
|
def event(self, *args, **kwargs):
|
|
|
|
"""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)
|
|
|
|
"""
|
|
|
|
if len(args) == 1 and len(kwargs) == 0 and callable(args[0]):
|
|
|
|
# the decorator was invoked without arguments
|
|
|
|
# args[0] is the decorated function
|
|
|
|
return self.on(args[0].__name__)(args[0])
|
|
|
|
else:
|
|
|
|
# the decorator was invoked with arguments
|
|
|
|
def set_handler(handler):
|
|
|
|
return self.on(handler.__name__, *args, **kwargs)(handler)
|
|
|
|
|
|
|
|
return set_handler
|
|
|
|
|
|
|
|
def register_namespace(self, namespace_handler):
|
|
|
|
"""Register a namespace handler object.
|
|
|
|
|
|
|
|
:param namespace_handler: An instance of a :class:`Namespace`
|
|
|
|
subclass that handles all the event traffic
|
|
|
|
for a namespace.
|
|
|
|
"""
|
|
|
|
if not isinstance(namespace_handler, namespace.ClientNamespace):
|
|
|
|
raise ValueError('Not a namespace instance')
|
|
|
|
if self.is_asyncio_based() != namespace_handler.is_asyncio_based():
|
|
|
|
raise ValueError('Not a valid namespace class for this client')
|
|
|
|
namespace_handler._set_client(self)
|
|
|
|
self.namespace_handlers[namespace_handler.namespace] = \
|
|
|
|
namespace_handler
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def connect(self, url, headers={}, auth=None, transports=None,
|
2021-05-08 14:25:29 +00:00
|
|
|
namespaces=None, socketio_path='socket.io', wait=True,
|
|
|
|
wait_timeout=1):
|
2020-01-30 01:07:26 +00:00
|
|
|
"""Connect to a Socket.IO server.
|
|
|
|
|
|
|
|
:param url: The URL of the Socket.IO server. It can include custom
|
2022-01-24 04:07:52 +00:00
|
|
|
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.
|
2020-01-30 01:07:26 +00:00
|
|
|
:param headers: A dictionary with custom headers to send with the
|
2022-01-24 04:07:52 +00:00
|
|
|
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.
|
2020-01-30 01:07:26 +00:00
|
|
|
: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.
|
2021-05-08 14:25:29 +00:00
|
|
|
:param namespaces: The namespaces to connect as a string or list of
|
|
|
|
strings. If not given, the namespaces that have
|
|
|
|
registered event handlers are connected.
|
2020-01-30 01:07:26 +00:00
|
|
|
:param socketio_path: The endpoint where the Socket.IO server is
|
|
|
|
installed. The default value is appropriate for
|
|
|
|
most cases.
|
2021-05-08 14:25:29 +00:00
|
|
|
:param wait: if set to ``True`` (the default) the call only returns
|
|
|
|
when all the namespaces are connected. If set to
|
|
|
|
``False``, the call returns as soon as the Engine.IO
|
|
|
|
transport is connected, and the namespaces will connect
|
|
|
|
in the background.
|
|
|
|
:param wait_timeout: How long the client should wait for the
|
|
|
|
connection. The default is 1 second. This
|
|
|
|
argument is only considered when ``wait`` is set
|
|
|
|
to ``True``.
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
Example usage::
|
|
|
|
|
|
|
|
sio = socketio.Client()
|
|
|
|
sio.connect('http://localhost:5000')
|
|
|
|
"""
|
2021-05-08 14:25:29 +00:00
|
|
|
if self.connected:
|
|
|
|
raise exceptions.ConnectionError('Already connected')
|
|
|
|
|
2020-01-30 01:07:26 +00:00
|
|
|
self.connection_url = url
|
|
|
|
self.connection_headers = headers
|
2022-01-24 04:07:52 +00:00
|
|
|
self.connection_auth = auth
|
2020-01-30 01:07:26 +00:00
|
|
|
self.connection_transports = transports
|
|
|
|
self.connection_namespaces = namespaces
|
|
|
|
self.socketio_path = socketio_path
|
|
|
|
|
|
|
|
if namespaces is None:
|
2021-05-08 14:25:29 +00:00
|
|
|
namespaces = list(set(self.handlers.keys()).union(
|
|
|
|
set(self.namespace_handlers.keys())))
|
|
|
|
if len(namespaces) == 0:
|
|
|
|
namespaces = ['/']
|
|
|
|
elif isinstance(namespaces, str):
|
2020-01-30 01:07:26 +00:00
|
|
|
namespaces = [namespaces]
|
2021-05-08 14:25:29 +00:00
|
|
|
self.connection_namespaces = namespaces
|
|
|
|
self.namespaces = {}
|
|
|
|
if self._connect_event is None:
|
|
|
|
self._connect_event = self.eio.create_event()
|
|
|
|
else:
|
|
|
|
self._connect_event.clear()
|
2022-01-24 04:07:52 +00:00
|
|
|
real_url = self._get_real_value(self.connection_url)
|
|
|
|
real_headers = self._get_real_value(self.connection_headers)
|
2020-01-30 01:07:26 +00:00
|
|
|
try:
|
2022-01-24 04:07:52 +00:00
|
|
|
self.eio.connect(real_url, headers=real_headers,
|
|
|
|
transports=transports,
|
2020-01-30 01:07:26 +00:00
|
|
|
engineio_path=socketio_path)
|
|
|
|
except engineio.exceptions.ConnectionError as exc:
|
2021-05-08 14:25:29 +00:00
|
|
|
self._trigger_event(
|
|
|
|
'connect_error', '/',
|
|
|
|
exc.args[1] if len(exc.args) > 1 else exc.args[0])
|
|
|
|
raise exceptions.ConnectionError(exc.args[0]) from None
|
|
|
|
|
|
|
|
if wait:
|
|
|
|
while self._connect_event.wait(timeout=wait_timeout):
|
|
|
|
self._connect_event.clear()
|
|
|
|
if set(self.namespaces) == set(self.connection_namespaces):
|
|
|
|
break
|
|
|
|
if set(self.namespaces) != set(self.connection_namespaces):
|
|
|
|
self.disconnect()
|
|
|
|
raise exceptions.ConnectionError(
|
|
|
|
'One or more namespaces failed to connect')
|
|
|
|
|
2020-01-30 01:07:26 +00:00
|
|
|
self.connected = True
|
|
|
|
|
|
|
|
def wait(self):
|
|
|
|
"""Wait until the connection with the server ends.
|
|
|
|
|
|
|
|
Client applications can use this function to block the main thread
|
|
|
|
during the life of the connection.
|
|
|
|
"""
|
|
|
|
while True:
|
|
|
|
self.eio.wait()
|
|
|
|
self.sleep(1) # give the reconnect task time to start up
|
|
|
|
if not self._reconnect_task:
|
|
|
|
break
|
|
|
|
self._reconnect_task.join()
|
|
|
|
if self.eio.state != 'connected':
|
|
|
|
break
|
|
|
|
|
|
|
|
def emit(self, event, data=None, namespace=None, callback=None):
|
|
|
|
"""Emit a custom event to one or more connected clients.
|
|
|
|
|
|
|
|
:param event: The event name. It can be any string. The event names
|
|
|
|
``'connect'``, ``'message'`` and ``'disconnect'`` are
|
|
|
|
reserved and should not be used.
|
2021-05-08 14:25:29 +00:00
|
|
|
: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.
|
2020-01-30 01:07:26 +00:00
|
|
|
:param namespace: The Socket.IO namespace for the event. If this
|
|
|
|
argument is omitted the event is emitted to the
|
|
|
|
default namespace.
|
|
|
|
:param callback: If given, this function will be called to acknowledge
|
2021-05-08 14:25:29 +00:00
|
|
|
the the server has received the message. The arguments
|
2020-01-30 01:07:26 +00:00
|
|
|
that will be passed to the function are those provided
|
2021-05-08 14:25:29 +00:00
|
|
|
by the server.
|
|
|
|
|
|
|
|
Note: this method is not thread safe. If multiple threads are emitting
|
|
|
|
at the same time on the same client connection, messages composed of
|
|
|
|
multiple packets may end up being sent in an incorrect sequence. Use
|
|
|
|
standard concurrency solutions (such as a Lock object) to prevent this
|
|
|
|
situation.
|
2020-01-30 01:07:26 +00:00
|
|
|
"""
|
|
|
|
namespace = namespace or '/'
|
2021-05-08 14:25:29 +00:00
|
|
|
if namespace not in self.namespaces:
|
2020-01-30 01:07:26 +00:00
|
|
|
raise exceptions.BadNamespaceError(
|
|
|
|
namespace + ' is not a connected namespace.')
|
|
|
|
self.logger.info('Emitting event "%s" [%s]', event, namespace)
|
|
|
|
if callback is not None:
|
|
|
|
id = self._generate_ack_id(namespace, callback)
|
|
|
|
else:
|
|
|
|
id = None
|
|
|
|
# tuples are expanded to multiple arguments, everything else is sent
|
|
|
|
# as a single argument
|
|
|
|
if isinstance(data, tuple):
|
|
|
|
data = list(data)
|
|
|
|
elif data is not None:
|
|
|
|
data = [data]
|
|
|
|
else:
|
|
|
|
data = []
|
2022-01-24 04:07:52 +00:00
|
|
|
self._send_packet(self.packet_class(packet.EVENT, namespace=namespace,
|
|
|
|
data=[event] + data, id=id))
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
def send(self, data, namespace=None, callback=None):
|
|
|
|
"""Send a message to one or more connected clients.
|
|
|
|
|
|
|
|
This function emits an event with the name ``'message'``. Use
|
|
|
|
:func:`emit` to issue custom event names.
|
|
|
|
|
2021-05-08 14:25:29 +00:00
|
|
|
: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.
|
2020-01-30 01:07:26 +00:00
|
|
|
:param namespace: The Socket.IO namespace for the event. If this
|
|
|
|
argument is omitted the event is emitted to the
|
|
|
|
default namespace.
|
|
|
|
:param callback: If given, this function will be called to acknowledge
|
2021-05-08 14:25:29 +00:00
|
|
|
the the server has received the message. The arguments
|
2020-01-30 01:07:26 +00:00
|
|
|
that will be passed to the function are those provided
|
2021-05-08 14:25:29 +00:00
|
|
|
by the server.
|
2020-01-30 01:07:26 +00:00
|
|
|
"""
|
|
|
|
self.emit('message', data=data, namespace=namespace,
|
|
|
|
callback=callback)
|
|
|
|
|
|
|
|
def call(self, event, data=None, namespace=None, timeout=60):
|
|
|
|
"""Emit a custom event to a client and wait for the response.
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
This method issues an emit with a callback and waits for the callback
|
|
|
|
to be invoked before returning. If the callback isn't invoked before
|
|
|
|
the timeout, then a ``TimeoutError`` exception is raised. If the
|
|
|
|
Socket.IO connection drops during the wait, this method still waits
|
|
|
|
until the specified timeout.
|
|
|
|
|
2020-01-30 01:07:26 +00:00
|
|
|
:param event: The event name. It can be any string. The event names
|
|
|
|
``'connect'``, ``'message'`` and ``'disconnect'`` are
|
|
|
|
reserved and should not be used.
|
2021-05-08 14:25:29 +00:00
|
|
|
: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.
|
2020-01-30 01:07:26 +00:00
|
|
|
:param namespace: The Socket.IO namespace for the event. If this
|
|
|
|
argument is omitted the event is emitted to the
|
|
|
|
default namespace.
|
|
|
|
:param timeout: The waiting timeout. If the timeout is reached before
|
|
|
|
the client acknowledges the event, then a
|
|
|
|
``TimeoutError`` exception is raised.
|
2021-05-08 14:25:29 +00:00
|
|
|
|
|
|
|
Note: this method is not thread safe. If multiple threads are emitting
|
|
|
|
at the same time on the same client connection, messages composed of
|
|
|
|
multiple packets may end up being sent in an incorrect sequence. Use
|
|
|
|
standard concurrency solutions (such as a Lock object) to prevent this
|
|
|
|
situation.
|
2020-01-30 01:07:26 +00:00
|
|
|
"""
|
|
|
|
callback_event = self.eio.create_event()
|
|
|
|
callback_args = []
|
|
|
|
|
|
|
|
def event_callback(*args):
|
|
|
|
callback_args.append(args)
|
|
|
|
callback_event.set()
|
|
|
|
|
|
|
|
self.emit(event, data=data, namespace=namespace,
|
|
|
|
callback=event_callback)
|
|
|
|
if not callback_event.wait(timeout=timeout):
|
|
|
|
raise exceptions.TimeoutError()
|
|
|
|
return callback_args[0] if len(callback_args[0]) > 1 \
|
|
|
|
else callback_args[0][0] if len(callback_args[0]) == 1 \
|
|
|
|
else None
|
|
|
|
|
|
|
|
def disconnect(self):
|
|
|
|
"""Disconnect from the server."""
|
|
|
|
# here we just request the disconnection
|
|
|
|
# later in _handle_eio_disconnect we invoke the disconnect handler
|
|
|
|
for n in self.namespaces:
|
2022-01-24 04:07:52 +00:00
|
|
|
self._send_packet(self.packet_class(
|
|
|
|
packet.DISCONNECT, namespace=n))
|
2020-01-30 01:07:26 +00:00
|
|
|
self.eio.disconnect(abort=True)
|
|
|
|
|
2021-05-08 14:25:29 +00:00
|
|
|
def get_sid(self, namespace=None):
|
|
|
|
"""Return the ``sid`` associated with a connection.
|
|
|
|
|
|
|
|
:param namespace: The Socket.IO namespace. If this argument is omitted
|
|
|
|
the handler is associated with the default
|
|
|
|
namespace. Note that unlike previous versions, the
|
|
|
|
current version of the Socket.IO protocol uses
|
|
|
|
different ``sid`` values per namespace.
|
|
|
|
|
|
|
|
This method returns the ``sid`` for the requested namespace as a
|
|
|
|
string.
|
|
|
|
"""
|
|
|
|
return self.namespaces.get(namespace or '/')
|
|
|
|
|
2020-01-30 01:07:26 +00:00
|
|
|
def transport(self):
|
|
|
|
"""Return the name of the transport used by the client.
|
|
|
|
|
|
|
|
The two possible values returned by this function are ``'polling'``
|
|
|
|
and ``'websocket'``.
|
|
|
|
"""
|
|
|
|
return self.eio.transport()
|
|
|
|
|
|
|
|
def start_background_task(self, target, *args, **kwargs):
|
|
|
|
"""Start a background task using the appropriate async model.
|
|
|
|
|
|
|
|
This is a utility function that applications can use to start a
|
|
|
|
background task using the method that is compatible with the
|
|
|
|
selected async mode.
|
|
|
|
|
|
|
|
:param target: the target function to execute.
|
|
|
|
:param args: arguments to pass to the function.
|
|
|
|
:param kwargs: keyword arguments to pass to the function.
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
This function returns an object that represents the background task,
|
|
|
|
on which the ``join()`` methond can be invoked to wait for the task to
|
|
|
|
complete.
|
2020-01-30 01:07:26 +00:00
|
|
|
"""
|
|
|
|
return self.eio.start_background_task(target, *args, **kwargs)
|
|
|
|
|
|
|
|
def sleep(self, seconds=0):
|
|
|
|
"""Sleep for the requested amount of time using the appropriate async
|
|
|
|
model.
|
|
|
|
|
|
|
|
This is a utility function that applications can use to put a task to
|
|
|
|
sleep without having to worry about using the correct call for the
|
|
|
|
selected async mode.
|
|
|
|
"""
|
|
|
|
return self.eio.sleep(seconds)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def _get_real_value(self, value):
|
|
|
|
"""Return the actual value, for parameters that can also be given as
|
|
|
|
callables."""
|
|
|
|
if not callable(value):
|
|
|
|
return value
|
|
|
|
return value()
|
|
|
|
|
2020-01-30 01:07:26 +00:00
|
|
|
def _send_packet(self, pkt):
|
|
|
|
"""Send a Socket.IO packet to the server."""
|
|
|
|
encoded_packet = pkt.encode()
|
|
|
|
if isinstance(encoded_packet, list):
|
|
|
|
for ep in encoded_packet:
|
2021-05-08 14:25:29 +00:00
|
|
|
self.eio.send(ep)
|
2020-01-30 01:07:26 +00:00
|
|
|
else:
|
2021-05-08 14:25:29 +00:00
|
|
|
self.eio.send(encoded_packet)
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
def _generate_ack_id(self, namespace, callback):
|
|
|
|
"""Generate a unique identifier for an ACK packet."""
|
|
|
|
namespace = namespace or '/'
|
|
|
|
if namespace not in self.callbacks:
|
|
|
|
self.callbacks[namespace] = {0: itertools.count(1)}
|
2021-05-08 14:25:29 +00:00
|
|
|
id = next(self.callbacks[namespace][0])
|
2020-01-30 01:07:26 +00:00
|
|
|
self.callbacks[namespace][id] = callback
|
|
|
|
return id
|
|
|
|
|
2021-05-08 14:25:29 +00:00
|
|
|
def _handle_connect(self, namespace, data):
|
2020-01-30 01:07:26 +00:00
|
|
|
namespace = namespace or '/'
|
2021-05-08 14:25:29 +00:00
|
|
|
if namespace not in self.namespaces:
|
|
|
|
self.logger.info('Namespace {} is connected'.format(namespace))
|
|
|
|
self.namespaces[namespace] = (data or {}).get('sid', self.sid)
|
|
|
|
self._trigger_event('connect', namespace=namespace)
|
|
|
|
self._connect_event.set()
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
def _handle_disconnect(self, namespace):
|
|
|
|
if not self.connected:
|
|
|
|
return
|
|
|
|
namespace = namespace or '/'
|
|
|
|
self._trigger_event('disconnect', namespace=namespace)
|
|
|
|
if namespace in self.namespaces:
|
2021-05-08 14:25:29 +00:00
|
|
|
del self.namespaces[namespace]
|
|
|
|
if not self.namespaces:
|
2020-01-30 01:07:26 +00:00
|
|
|
self.connected = False
|
2021-05-08 14:25:29 +00:00
|
|
|
self.eio.disconnect(abort=True)
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
def _handle_event(self, namespace, id, data):
|
|
|
|
namespace = namespace or '/'
|
|
|
|
self.logger.info('Received event "%s" [%s]', data[0], namespace)
|
|
|
|
r = self._trigger_event(data[0], namespace, *data[1:])
|
|
|
|
if id is not None:
|
|
|
|
# send ACK packet with the response returned by the handler
|
|
|
|
# tuples are expanded as multiple arguments
|
|
|
|
if r is None:
|
|
|
|
data = []
|
|
|
|
elif isinstance(r, tuple):
|
|
|
|
data = list(r)
|
|
|
|
else:
|
|
|
|
data = [r]
|
2022-01-24 04:07:52 +00:00
|
|
|
self._send_packet(self.packet_class(
|
|
|
|
packet.ACK, namespace=namespace, id=id, data=data))
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
def _handle_ack(self, namespace, id, data):
|
|
|
|
namespace = namespace or '/'
|
|
|
|
self.logger.info('Received ack [%s]', namespace)
|
|
|
|
callback = None
|
|
|
|
try:
|
|
|
|
callback = self.callbacks[namespace][id]
|
|
|
|
except KeyError:
|
|
|
|
# if we get an unknown callback we just ignore it
|
|
|
|
self.logger.warning('Unknown callback received, ignoring.')
|
|
|
|
else:
|
|
|
|
del self.callbacks[namespace][id]
|
|
|
|
if callback is not None:
|
|
|
|
callback(*data)
|
|
|
|
|
|
|
|
def _handle_error(self, namespace, data):
|
|
|
|
namespace = namespace or '/'
|
|
|
|
self.logger.info('Connection to namespace {} was rejected'.format(
|
|
|
|
namespace))
|
|
|
|
if data is None:
|
|
|
|
data = tuple()
|
|
|
|
elif not isinstance(data, (tuple, list)):
|
|
|
|
data = (data,)
|
|
|
|
self._trigger_event('connect_error', namespace, *data)
|
2021-05-08 14:25:29 +00:00
|
|
|
self._connect_event.set()
|
2020-01-30 01:07:26 +00:00
|
|
|
if namespace in self.namespaces:
|
2021-05-08 14:25:29 +00:00
|
|
|
del self.namespaces[namespace]
|
2020-01-30 01:07:26 +00:00
|
|
|
if namespace == '/':
|
2021-05-08 14:25:29 +00:00
|
|
|
self.namespaces = {}
|
2020-01-30 01:07:26 +00:00
|
|
|
self.connected = False
|
|
|
|
|
|
|
|
def _trigger_event(self, event, namespace, *args):
|
|
|
|
"""Invoke an application event handler."""
|
|
|
|
# first see if we have an explicit handler for the event
|
2022-01-24 04:07:52 +00:00
|
|
|
if namespace in self.handlers:
|
|
|
|
if event in self.handlers[namespace]:
|
|
|
|
return self.handlers[namespace][event](*args)
|
|
|
|
elif event not in self.reserved_events and \
|
|
|
|
'*' in self.handlers[namespace]:
|
|
|
|
return self.handlers[namespace]['*'](event, *args)
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
# or else, forward the event to a namespace handler if one exists
|
|
|
|
elif namespace in self.namespace_handlers:
|
|
|
|
return self.namespace_handlers[namespace].trigger_event(
|
|
|
|
event, *args)
|
|
|
|
|
|
|
|
def _handle_reconnect(self):
|
2021-05-08 14:25:29 +00:00
|
|
|
if self._reconnect_abort is None: # pragma: no cover
|
|
|
|
self._reconnect_abort = self.eio.create_event()
|
2020-01-30 01:07:26 +00:00
|
|
|
self._reconnect_abort.clear()
|
|
|
|
reconnecting_clients.append(self)
|
|
|
|
attempt_count = 0
|
|
|
|
current_delay = self.reconnection_delay
|
|
|
|
while True:
|
|
|
|
delay = current_delay
|
|
|
|
current_delay *= 2
|
|
|
|
if delay > self.reconnection_delay_max:
|
|
|
|
delay = self.reconnection_delay_max
|
|
|
|
delay += self.randomization_factor * (2 * random.random() - 1)
|
|
|
|
self.logger.info(
|
|
|
|
'Connection failed, new attempt in {:.02f} seconds'.format(
|
|
|
|
delay))
|
|
|
|
if self._reconnect_abort.wait(delay):
|
|
|
|
self.logger.info('Reconnect task aborted')
|
|
|
|
break
|
|
|
|
attempt_count += 1
|
|
|
|
try:
|
|
|
|
self.connect(self.connection_url,
|
|
|
|
headers=self.connection_headers,
|
2022-01-24 04:07:52 +00:00
|
|
|
auth=self.connection_auth,
|
2020-01-30 01:07:26 +00:00
|
|
|
transports=self.connection_transports,
|
|
|
|
namespaces=self.connection_namespaces,
|
|
|
|
socketio_path=self.socketio_path)
|
|
|
|
except (exceptions.ConnectionError, ValueError):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.logger.info('Reconnection successful')
|
|
|
|
self._reconnect_task = None
|
|
|
|
break
|
|
|
|
if self.reconnection_attempts and \
|
|
|
|
attempt_count >= self.reconnection_attempts:
|
|
|
|
self.logger.info(
|
|
|
|
'Maximum reconnection attempts reached, giving up')
|
|
|
|
break
|
|
|
|
reconnecting_clients.remove(self)
|
|
|
|
|
|
|
|
def _handle_eio_connect(self):
|
|
|
|
"""Handle the Engine.IO connection event."""
|
|
|
|
self.logger.info('Engine.IO connection established')
|
|
|
|
self.sid = self.eio.sid
|
2022-11-07 18:06:49 +00:00
|
|
|
real_auth = self._get_real_value(self.connection_auth) or {}
|
2021-05-08 14:25:29 +00:00
|
|
|
for n in self.connection_namespaces:
|
2022-01-24 04:07:52 +00:00
|
|
|
self._send_packet(self.packet_class(
|
|
|
|
packet.CONNECT, data=real_auth, namespace=n))
|
2020-01-30 01:07:26 +00:00
|
|
|
|
|
|
|
def _handle_eio_message(self, data):
|
|
|
|
"""Dispatch Engine.IO messages."""
|
|
|
|
if self._binary_packet:
|
|
|
|
pkt = self._binary_packet
|
|
|
|
if pkt.add_attachment(data):
|
|
|
|
self._binary_packet = None
|
|
|
|
if pkt.packet_type == packet.BINARY_EVENT:
|
|
|
|
self._handle_event(pkt.namespace, pkt.id, pkt.data)
|
|
|
|
else:
|
|
|
|
self._handle_ack(pkt.namespace, pkt.id, pkt.data)
|
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
pkt = self.packet_class(encoded_packet=data)
|
2020-01-30 01:07:26 +00:00
|
|
|
if pkt.packet_type == packet.CONNECT:
|
2021-05-08 14:25:29 +00:00
|
|
|
self._handle_connect(pkt.namespace, pkt.data)
|
2020-01-30 01:07:26 +00:00
|
|
|
elif pkt.packet_type == packet.DISCONNECT:
|
|
|
|
self._handle_disconnect(pkt.namespace)
|
|
|
|
elif pkt.packet_type == packet.EVENT:
|
|
|
|
self._handle_event(pkt.namespace, pkt.id, pkt.data)
|
|
|
|
elif pkt.packet_type == packet.ACK:
|
|
|
|
self._handle_ack(pkt.namespace, pkt.id, pkt.data)
|
|
|
|
elif pkt.packet_type == packet.BINARY_EVENT or \
|
|
|
|
pkt.packet_type == packet.BINARY_ACK:
|
|
|
|
self._binary_packet = pkt
|
2021-05-08 14:25:29 +00:00
|
|
|
elif pkt.packet_type == packet.CONNECT_ERROR:
|
2020-01-30 01:07:26 +00:00
|
|
|
self._handle_error(pkt.namespace, pkt.data)
|
|
|
|
else:
|
|
|
|
raise ValueError('Unknown packet type.')
|
|
|
|
|
|
|
|
def _handle_eio_disconnect(self):
|
|
|
|
"""Handle the Engine.IO disconnection event."""
|
|
|
|
self.logger.info('Engine.IO connection dropped')
|
|
|
|
if self.connected:
|
|
|
|
for n in self.namespaces:
|
|
|
|
self._trigger_event('disconnect', namespace=n)
|
2021-05-08 14:25:29 +00:00
|
|
|
self.namespaces = {}
|
2020-01-30 01:07:26 +00:00
|
|
|
self.connected = False
|
|
|
|
self.callbacks = {}
|
|
|
|
self._binary_packet = None
|
|
|
|
self.sid = None
|
|
|
|
if self.eio.state == 'connected' and self.reconnection:
|
|
|
|
self._reconnect_task = self.start_background_task(
|
|
|
|
self._handle_reconnect)
|
|
|
|
|
|
|
|
def _engineio_client_class(self):
|
|
|
|
return engineio.Client
|