This commit is contained in:
eric
2025-08-04 14:12:40 +08:00
parent 36b70d2135
commit 1eb1150b84
3 changed files with 53 additions and 25 deletions

View File

@@ -17,10 +17,10 @@ async def ws_handler(websocket):
connected_clients.remove(websocket)
# 推送消息到所有前端
async def push_to_all_clients(user_data: dict):
async def push_to_all_clients(user_data: dict,type: str = "join"):
# ✅ 直接发送结构化 JSON
message = {
"type": "join",
"type": type,
"data": user_data
}
for client in connected_clients.copy():
@@ -35,12 +35,12 @@ async def ws_main():
await asyncio.Future() # 永远挂起,保持服务运行
# 主线程中调用这个方法推送消息
def push_to_frontend(user_data):
def push_to_frontend(user_data, type: str = "join"):
loop = asyncio.get_event_loop()
if loop.is_running():
asyncio.run_coroutine_threadsafe(push_to_all_clients(user_data), loop)
asyncio.run_coroutine_threadsafe(push_to_all_clients(user_data, type), loop)
else:
loop.run_until_complete(push_to_all_clients(user_data))
loop.run_until_complete(push_to_all_clients(user_data, type))
# 在线程中启动服务
def start_ws_server():