async def is_websocket_active(ws: WebSocket) -> bool: if not (ws.application_state == WebSocketState.CONNECTED and ws.client_state == WebSocketState.CONNECTED): return False try: await asyncio.wait_for(ws.send_json({'type': 'ping'}), HEART_BEAT_INTERVAL) message = await asyncio.wait_for(ws.receive_json(), HEART_BEAT_INTERVAL) assert message['type'] == 'pong' except BaseException: # asyncio.TimeoutError and ws.close() return False return True
def _init(ws: WebSocket): ws.send_json({"type": "connection_init"}) assert ws.receive_json() == {"type": "connection_ack"}
def _init(ws: WebSocket) -> None: ws.send_json({"type": "connection_init"}) # type: ignore assert ws.receive_json() == {"type": "connection_ack"}