async def receive(self) -> None: # TODO: preferably this would be an infinite loop that waits on # self.connected_event.wait(), but it seems that `async for` never # throws an exception, even when the websocket gets disconnected. logger.trace("Started receiving on websocket.") ws = self.ws assert ws is not None async for next_message in ws: logger.trace("Received message on websocket: {}", next_message) # process response messages # noinspection PyBroadException try: response_message = JsonRpcResponse.from_jsons(next_message) # pylint: disable=broad-except except Exception: pass else: await self.response_messages.put(response_message) continue # process notification messages try: subscription_message = JsonRpcRequest.from_jsons(next_message) params = subscription_message.params assert isinstance(params, dict) await self.subscription_manager.receive_message( SubscriptionNotification( params["subscription"], params["result"], )) # pylint: disable=broad-except except Exception as e: logger.warning(log_messages.ETH_RPC_PROCESSING_ERROR, next_message, e, exc_info=True) logger.trace( "Temporarily stopped receiving message on websocket. Awaiting reconnection." )
async def consumer(ws, _path): try: async for message in ws: rpc_request = JsonRpcRequest.from_jsons(message) if rpc_request.method_name == "eth_subscribe": await ws.send( JsonRpcResponse(rpc_request.id, self.eth_subscription_id).to_jsons() ) elif rpc_request.method_name == "eth_getTransactionByHash": nonce = int(rpc_request.id) await ws.send( JsonRpcResponse( rpc_request.id, tx_to_eth_rpc_json(self.sample_transactions[nonce]) ).to_jsons() ) except Exception as e: # server closed, exit pass