""" Cliente de teste para versão otimizada (WebSocket binário). """ import asyncio import aiohttp import json import struct import sys # Tipos de mensagem binária MSG_FRAME = 0x01 MSG_AUDIO = 0x02 MSG_AUDIO_CHUNK = 0x03 async def test_optimized(text: str, voice: str = "tara"): """Testa o servidor otimizado com WebSocket binário.""" ws_url = "ws://localhost:8080/ws" print(f"Conectando a {ws_url}...") timeout = aiohttp.ClientTimeout(total=300) async with aiohttp.ClientSession(timeout=timeout) as session: async with session.ws_connect(ws_url, max_msg_size=50*1024*1024) as ws: print("Conectado!") # Enviar requisição request = { "action": "generate", "text": text, "voice": voice } print(f"Enviando: {text[:50]}...") await ws.send_json(request) frames_received = 0 total_bytes = 0 audio_received = False async for msg in ws: if msg.type == aiohttp.WSMsgType.TEXT: data = json.loads(msg.data) msg_type = data.get("type", "") if msg_type == "status": print(f" Status: {data.get('message')}") elif msg_type == "first_frame": print(f" Primeiro frame: {data.get('latency_ms')}ms") elif msg_type == "done": print(f"\n Concluído!") print(f" Frames: {data.get('total_frames')}") print(f" Tempo: {data.get('elapsed_ms')}ms") print(f" Bytes enviados: {data.get('bytes_sent', 0)/1024:.1f}KB") break elif msg_type == "error": print(f" ERRO: {data.get('message')}") break elif msg.type == aiohttp.WSMsgType.BINARY: # Mensagem binária buffer = msg.data view = memoryview(buffer) msg_type = buffer[0] if msg_type == MSG_FRAME: # [tipo:1][index:4][tamanho:4][dados] frame_index, data_size = struct.unpack_from(' 1: text = sys.argv[1] voice = "tara" if len(sys.argv) > 2: voice = sys.argv[2] asyncio.run(test_optimized(text, voice))