Пример #1
0
    async def _emit_with_result(self, event, data=None, namespace=None):
        if self.connection_state.is_disconnected:
            self.logger.debug(f"{event}, urgent connect start")
            await self.connect_to_server()
            self.logger.debug(f"{event}, urgent connect finished")

        self.logger.debug(f"{event}, getting lock")
        async with self._call_lock:
            request_start = time.time()
            timeout = self._current_timeout
            self.logger.debug(f"{event}, will call with timeout {timeout}")
            try:
                result = await self.sio.call(event,
                                             data,
                                             namespace=namespace,
                                             timeout=timeout)
                request_time = time.time() - request_start
                self._update_timeout_with(request_time, True)

            except socketio.exceptions.TimeoutError:
                request_time = time.time() - request_start
                self._update_timeout_with(request_time, False)
                raise RequestTimeout(
                    f"Timeout after {request_time:.2f}s, with a timeout of {timeout}."
                )

        if result is None:
            return None

        possible_error = decode_error(result)
        if possible_error is None:
            return result["result"]
        else:
            raise possible_error
Пример #2
0
    async def _emit_with_result(self,
                                event: str,
                                data=None,
                                namespace=None,
                                *,
                                handle_invalid_session: bool = True):
        if self.connection_state.is_disconnected:
            self.logger.debug(f"{event}, urgent connect start")
            await self.connect_to_server()
            self.logger.debug(f"{event}, urgent connect finished")

        self.logger.debug(f"{event}, getting lock")
        async with self._call_lock:
            request_start = time.time()
            timeout = self._current_timeout
            self.logger.debug(f"{event}, will call with timeout {timeout}")
            try:
                result = await self.sio.call(event,
                                             data,
                                             namespace=namespace,
                                             timeout=timeout)
                request_time = time.time() - request_start
                self._update_timeout_with(request_time, True)

            except socketio.exceptions.TimeoutError:
                request_time = time.time() - request_start
                self._update_timeout_with(request_time, False)
                if self._num_emit_failures >= _TIMEOUTS_TO_DISCONNECT:
                    # If getting too many timeouts in a row, just disconnect so the user is aware something is wrong.
                    await self.disconnect_from_server()
                raise error.RequestTimeout(
                    f"Timeout after {request_time:.2f}s, with a timeout of {timeout}."
                )

        if result is None:
            return None

        possible_error = error.decode_error(result)
        if possible_error is None:
            return result["result"]
        else:
            if handle_invalid_session and isinstance(possible_error,
                                                     error.InvalidSession):
                self.logger.info("Received InvalidSession during a %s call",
                                 event)
                await self.logout()

            raise possible_error
Пример #3
0
    async def _emit_with_result(self, event, data=None, namespace=None):
        if self.connection_state.is_disconnected:
            self.logger.debug(f"{event}, urgent connect start")
            await self.connect_to_server()
            self.logger.debug(f"{event}, urgent connect finished")

        self.logger.debug(f"{event}, getting lock")
        async with self._call_lock:
            self.logger.debug(f"{event}, will call")
            result = await self.sio.call(event,
                                         data,
                                         namespace=namespace,
                                         timeout=30)

        if result is None:
            return None

        possible_error = decode_error(result)
        if possible_error is None:
            return result["result"]
        else:
            raise possible_error