更新最新代码

This commit is contained in:
2024-03-21 20:32:50 +08:00
parent 1ec018ca90
commit 7d756bcfa4
52 changed files with 38968 additions and 51 deletions
+22
View File
@@ -0,0 +1,22 @@
import asyncio
import websockets
# 存储所有连接的客户端
connected_clients = set()
async def broadcast(message):
if connected_clients:
await asyncio.wait([client.send(message) for client in connected_clients])
async def handler(websocket, path):
connected_clients.add(websocket)
try:
async for message in websocket:
await broadcast(message)
finally:
connected_clients.discard(websocket)
start_server = websockets.serve(handler, "localhost", 8765)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()