-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWebSocket-Simple-ASYNC.py
More file actions
30 lines (25 loc) · 981 Bytes
/
WebSocket-Simple-ASYNC.py
File metadata and controls
30 lines (25 loc) · 981 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import asyncio
import websockets
# Lista para almacenar las conexiones de los clientes
clientes = set()
async def manejar_mensaje(mensaje, cliente):
# Enviar el mensaje a todos los clientes conectados
for cliente_conectado in clientes:
if cliente_conectado != cliente:
await cliente_conectado.send(mensaje)
async def servidor(websocket, path):
# Agregar nuevo cliente a la lista
clientes.add(websocket)
try:
# Bucle para manejar los mensajes del cliente
async for mensaje in websocket:
# Procesar el mensaje recibido del cliente
await manejar_mensaje(mensaje, websocket)
finally:
# Eliminar cliente cuando se desconecte
clientes.remove(websocket)
# Iniciar el servidor WebSocket en el puerto 8765
start_server = websockets.serve(servidor, "localhost", 8765)
# Correr el servidor
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()