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: 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[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=Constants.MESSAGE_TIMEOUT) yield msg except asyncio.TimeoutError: self.logger().warning( "WebSocket message timed out. Going to reconnect...") return except 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=60.0) yield raw_msg except asyncio.TimeoutError: try: pong_waiter = await client.ping() await asyncio.wait_for(pong_waiter, timeout=60.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]: # 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: 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]: """ 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") else: yield msg except websockets.exceptions.ConnectionClosed: self.logger().warning( "Loopring websocket connection closed. Reconnecting...") return finally: await ws.close()
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) self._last_recv_time = time.time() yield msg except asyncio.TimeoutError: pong_waiter = await ws.ping() 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.exceptions.ConnectionClosed: return finally: await ws.close()
async def keep_alive(socket: websockets.WebSocketClientProtocol): while True: ping = input('{"op":"ping"}') socket.send(ping) await asyncio.sleep(3600)