更新最新代码

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
+28
View File
@@ -0,0 +1,28 @@
import asyncio
import websockets
import json
connected = set()
async def handler(websocket):
# 当新的客户端连接时,添加到连接集合
connected.add(websocket)
try:
async for message in websocket:
data = json.loads(message)
if data['type'] == 'edit':
# 广播编辑消息到所有连接的WebSocket
await broadcast(data['content'])
finally:
# 当客户端断开连接时,移除
connected.remove(websocket)
async def broadcast(message):
# 往所有客户端广播消息
for ws in connected:
await ws.send(message)
start_server = websockets.serve(handler, "localhost", 6789)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()