41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
import asyncio
|
|
import json
|
|
from websockets import connect
|
|
import httpx
|
|
|
|
async def test():
|
|
print("=== Test Simple ===")
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
r = await client.post('http://localhost:3030/auth/login',
|
|
json={'username': 'admin', 'password': 'admin'})
|
|
token = r.json()['token']
|
|
|
|
async with connect('ws://localhost:3030/ws/chat') as ws:
|
|
await ws.send(json.dumps({'token': token}))
|
|
await ws.recv()
|
|
|
|
await ws.send(json.dumps({'type': 'create_session', 'name': 's'}))
|
|
created = json.loads(await ws.recv())
|
|
sid = created['session_id']
|
|
|
|
await ws.send(json.dumps({'type': 'connect_session', 'session_id': sid}))
|
|
await ws.recv()
|
|
|
|
print("Enviando: di hola")
|
|
await ws.send(json.dumps({'type': 'message', 'content': 'di hola'}))
|
|
|
|
print("Esperando respuesta (60s max)...")
|
|
for i in range(60):
|
|
try:
|
|
m = await asyncio.wait_for(ws.recv(), timeout=1.0)
|
|
data = json.loads(m)
|
|
print(f" RECIBIDO: {data}")
|
|
if data.get('type') == 'done':
|
|
print("\n=== COMPLETADO ===")
|
|
break
|
|
except asyncio.TimeoutError:
|
|
print(f" {i+1}s...", end="\r")
|
|
|
|
asyncio.run(test())
|