async def _inner_messages( self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: """ Generator function that returns messages from the web socket stream :param ws: current web socket connection :returns: message in AsyncIterable format """ # Terminate the recv() loop as soon as the next message timed out, so the outer loop can reconnect. try: while True: try: msg: str = await asyncio.wait_for( ws.recv(), timeout=self.MESSAGE_TIMEOUT) yield msg except asyncio.TimeoutError: try: await ws.send('{"type": "ping"}') except asyncio.TimeoutError: raise except asyncio.TimeoutError: self.logger().warning( "WebSocket ping timed out. Going to reconnect...") return except ConnectionClosed: return finally: await ws.close()
async def _inner_messages( self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: # Terminate the recv() loop as soon as the next message timed out, so the outer loop can reconnect. try: while True: try: msg: str = await asyncio.wait_for( ws.recv(), timeout=self.MESSAGE_TIMEOUT) yield msg except asyncio.TimeoutError: try: pong_waiter = await ws.ping() await asyncio.wait_for(pong_waiter, timeout=self.PING_TIMEOUT) except asyncio.TimeoutError: raise except asyncio.TimeoutError: self.logger().warning( "WebSocket ping timed out. Going to reconnect...") return except ConnectionClosed: return finally: await ws.close()
async def _inner_messages( self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: # Terminate the recv() loop as soon as the next message timed out, so the outer loop can reconnect. try: while True: try: raw_msg: str = await asyncio.wait_for( ws.recv(), timeout=self.MESSAGE_TIMEOUT) yield raw_msg except asyncio.TimeoutError: payload = {"op": CONSTANTS.PONG_ENDPOINT_NAME} pong_waiter = ws.send(ujson.dumps(payload)) async with self._throttler.execute_task( CONSTANTS.PONG_ENDPOINT_NAME): await asyncio.wait_for(pong_waiter, timeout=self.PING_TIMEOUT) self._last_recv_time = time.time() except asyncio.TimeoutError: self.logger().warning( "WebSocket ping timed out. Going to reconnect...") return except websockets.ConnectionClosed: return finally: await ws.close()
async def ws_messages( self, client: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: try: while True: try: raw_msg: str = await asyncio.wait_for(client.recv(), timeout=50.0) self._last_recv_time = time.time() yield raw_msg except asyncio.TimeoutError: try: self._last_recv_time = time.time() pong_waiter = await client.ping() await asyncio.wait_for(pong_waiter, timeout=50.0) except asyncio.TimeoutError: raise except asyncio.TimeoutError: self.logger().warning( "Websocket ping timed out. Going to reconnect... ") return except ConnectionClosed: return finally: await client.close()
async def _inner_messages( self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: """ Generator function that returns messages from the web socket stream :param ws: current web socket connection :returns: message in AsyncIterable format """ # Terminate the recv() loop as soon as the next message timed out, so the outer loop can reconnect. try: while True: msg: str = await asyncio.wait_for( ws.recv(), timeout=None ) # This will throw the ConnectionClosed exception on disconnect if msg == "ping": await ws.send( "pong" ) # skip returning this and handle this protocol level message here else: yield msg except websockets.exceptions.ConnectionClosed: self.logger().warning( "Loopring websocket connection closed. Reconnecting...") return finally: await ws.close()
async def __consumer_handler( self, websocket_client_connection: websockets.WebSocketClientProtocol): if websocket_client_connection is None: return worker_id = str( websocket_client_connection.request_headers.get_all("Origin")[0]) logger.info("Consumer handler of {} starting", str(worker_id)) while websocket_client_connection.open: message = None try: message = await asyncio.wait_for( websocket_client_connection.recv(), timeout=4.0) except asyncio.TimeoutError as te: await asyncio.sleep(0.02) except websockets.exceptions.ConnectionClosed as cc: logger.warning("Connection to {} was closed, stopping worker", str(worker_id)) async with self.__users_mutex: worker = self.__current_users.get(worker_id, None) if worker is not None: # TODO: do it abruptly in the worker, maybe set a flag to be checked for in send_and_wait to # TODO: throw an exception worker[1].stop_worker() await self.__internal_clean_up_user(worker_id, None) return if message is not None: await self.__on_message(message) logger.warning("Connection of {} closed in consumer_handler", str(worker_id))
async def _inner_messages( self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: # Terminate the recv() loop as soon as the next message timed out, so the outer loop can reconnect. try: while True: try: msg: str = await asyncio.wait_for( ws.recv(), timeout=self.MESSAGE_TIMEOUT) if ((msg != "{\"event\":\"heartbeat\"}" and "\"event\":\"systemStatus\"" not in msg and "\"event\":\"subscriptionStatus\"" not in msg)): yield msg except asyncio.TimeoutError: pong_waiter = await ws.ping() await asyncio.wait_for(pong_waiter, timeout=self.PING_TIMEOUT) except asyncio.TimeoutError: self.logger().warning( "WebSocket ping timed out. Going to reconnect...") return except ConnectionClosed: return finally: await ws.close()
async def _inner_messages( self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[Any]: # Terminate the recv() loop as soon as the next message timed out, so the outer loop can reconnect. try: while True: try: msg: str = await asyncio.wait_for( ws.recv(), timeout=self.MESSAGE_TIMEOUT) msg_json = ujson.loads(msg) event = msg_json["event"] if event == "push": self._last_recv_time = time.time() yield msg_json except asyncio.TimeoutError: try: await ws.send('{"command":"ping"}') self._last_recv_time = time.time() except asyncio.TimeoutError: raise except asyncio.TimeoutError: self.logger().warning( "WebSocket ping timed out. Going to reconnect...") return except websockets.exceptions.ConnectionClosed: return finally: await ws.close()
async def _inner_messages( self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: """ Generator function that returns messages from the web socket stream :param ws: current web socket connection :returns: message in AsyncIterable format """ # Terminate the recv() loop as soon as the next message timed out, so the outer loop can reconnect. try: while True: try: msg: str = await asyncio.wait_for(ws.recv(), timeout=MESSAGE_TIMEOUT) if (("heartbeat" not in msg and "systemStatus" not in msg and "subscriptionStatus" not in msg)): yield msg except asyncio.TimeoutError: try: pong_waiter = await ws.ping() await asyncio.wait_for(pong_waiter, timeout=PING_TIMEOUT) except asyncio.TimeoutError: raise except asyncio.TimeoutError: self.logger().warning( "WebSocket ping timed out. Going to reconnect...") return finally: await ws.close()
async def _inner_messages( self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: # Terminate the recv() loop as soon as the next message timed out, so the outer loop can reconnect. while True: try: msg: str = await asyncio.wait_for(ws.recv(), timeout=self.MESSAGE_TIMEOUT) yield msg except asyncio.TimeoutError: pong_waiter = await ws.ping() await asyncio.wait_for(pong_waiter, timeout=self.PING_TIMEOUT)
async def _inner_messages(self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: try: while True: msg: str = await asyncio.wait_for(ws.recv(), timeout=self.MESSAGE_TIMEOUT) yield msg except asyncio.TimeoutError: pong_waiter = await ws.ping() await asyncio.wait_for(pong_waiter, timeout=self.PING_TIMEOUT) except websockets.exceptions.ConnectionClosed: return finally: await ws.close()
async def ws_messages( self, client: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: try: while True: try: raw_msg: str = await asyncio.wait_for(client.recv(), timeout=30.0) yield raw_msg except asyncio.TimeoutError: await client.pong(data=b'') except ConnectionClosed: return finally: await client.close()
async def _get_response(self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: try: while True: try: msg: str = await asyncio.wait_for(ws.recv(), timeout=self.MESSAGE_TIMEOUT) yield msg except asyncio.TimeoutError: raise except asyncio.TimeoutError: self.logger().warning("WebSocket ping timed out. Going to reconnect...") return except ConnectionClosed: return finally: await ws.close()
async def conn_handle(self, wsocket: websockets.WebSocketClientProtocol, path: str): host, port = wsocket.remote_address log.debug(f'{host}:{port} websocket has connected') while not wsocket.closed: try: result = await asyncio.wait_for(wsocket.recv(), timeout=5) print() log.debug(f"From {host}:{port} recived: {result}") except asyncio.TimeoutError: continue except (asyncio.CancelledError, websockets.ConnectionClosed): log.debug( f"{host}:{port} websocket has disconnected.{wsocket.close_code}:'{wsocket.close_reason}'" )
async def __task_reader(self, ws:websockets.WebSocketClientProtocol, queue:asyncio.Queue)->None: self.logger.debug("[reader] Started") while not ws.closed: # Ждем сообщение flag,args = await wait_first([ ws.recv(), ws.wait_closed(), self.__estop.wait() ]) if not flag: break # Обрабатываем сообщение try: await self.on_recv(args, queue) except Exception as e: self.logger.warn(f"[reader] on_recv() failed: <{type(e).__name__}> {e}, flag: {flag}, args: {args}") # Закрываем подключение if not ws.closed: self.logger.info("[reader] Close connection") await ws.close() # Журнал self.logger.debug("[reader] Stopped")
async def _inner_messages( self, ws: websockets.WebSocketClientProtocol) -> AsyncIterable[str]: try: while True: try: msg: str = await asyncio.wait_for( ws.recv(), timeout=self.MESSAGE_TIMEOUT) msg = bitmart_utils.decompress_ws_message(msg) yield msg except asyncio.TimeoutError: pong_waiter = await ws.ping() await asyncio.wait_for(pong_waiter, timeout=self.PING_TIMEOUT) except asyncio.TimeoutError: self.logger().warning( "WebSocket ping timed out. Going to reconnect...") return except websockets.exceptions.ConnectionClosed: return finally: await ws.close()