|
|
@@ -1,7 +1,11 @@
|
|
|
# This Python file uses the following encoding: utf-8
|
|
|
# @author runhey
|
|
|
# github https://github.com/runhey
|
|
|
-from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
|
|
+from typing import Iterable
|
|
|
+
|
|
|
+from fastapi import WebSocket, WebSocketDisconnect
|
|
|
+from starlette.websockets import WebSocketState
|
|
|
+
|
|
|
|
|
|
class ScriptWSManager:
|
|
|
|
|
|
@@ -11,41 +15,69 @@ class ScriptWSManager:
|
|
|
async def connect(self, ws: WebSocket):
|
|
|
# 等待连接
|
|
|
await ws.accept()
|
|
|
- self.active_connections.append(ws)
|
|
|
+ if ws not in self.active_connections:
|
|
|
+ self.active_connections.append(ws)
|
|
|
|
|
|
async def disconnect(self, ws: WebSocket):
|
|
|
- # 关闭时 移除ws对象
|
|
|
- self.active_connections.remove(ws)
|
|
|
+ # 关闭时移除 ws 对象,重复调用也应安全
|
|
|
+ if ws in self.active_connections:
|
|
|
+ self.active_connections.remove(ws)
|
|
|
+ if ws.client_state == WebSocketState.DISCONNECTED:
|
|
|
+ return
|
|
|
try:
|
|
|
# 给前端发送最后一次关闭信号
|
|
|
await ws.close()
|
|
|
- except RuntimeError as e:
|
|
|
- print(e)
|
|
|
+ except (RuntimeError, WebSocketDisconnect):
|
|
|
+ return
|
|
|
+ except Exception:
|
|
|
+ return
|
|
|
+
|
|
|
+ async def send_text(self, ws: WebSocket, message: str) -> bool:
|
|
|
+ return await self._send(ws, ws.send_text, message)
|
|
|
+
|
|
|
+ async def send_json(self, ws: WebSocket, data: dict) -> bool:
|
|
|
+ return await self._send(ws, ws.send_json, data)
|
|
|
|
|
|
async def broadcast(self, message: str):
|
|
|
# 广播消息
|
|
|
- for connection in self.active_connections:
|
|
|
- try:
|
|
|
- await connection.send_text(message)
|
|
|
- except RuntimeError:
|
|
|
- await self.disconnect(connection)
|
|
|
+ await self._broadcast(self.active_connections, lambda connection: connection.send_text(message))
|
|
|
|
|
|
async def broadcast_state(self, data: dict):
|
|
|
# 广播自身的状态
|
|
|
- for connection in self.active_connections:
|
|
|
- try:
|
|
|
- await connection.send_json(data)
|
|
|
- except RuntimeError:
|
|
|
- await self.disconnect(connection)
|
|
|
+ await self._broadcast(self.active_connections, lambda connection: connection.send_json(data))
|
|
|
|
|
|
async def broadcast_log(self, log: str):
|
|
|
# 广播日志
|
|
|
- for connection in self.active_connections:
|
|
|
- try:
|
|
|
- await connection.send_text(log)
|
|
|
- except RuntimeError:
|
|
|
- await self.disconnect(connection)
|
|
|
+ await self._broadcast(self.active_connections, lambda connection: connection.send_text(log))
|
|
|
|
|
|
+ async def _send(self, ws: WebSocket, sender, payload) -> bool:
|
|
|
+ if ws.client_state == WebSocketState.DISCONNECTED:
|
|
|
+ await self.disconnect(ws)
|
|
|
+ return False
|
|
|
+ try:
|
|
|
+ await sender(payload)
|
|
|
+ return True
|
|
|
+ except (RuntimeError, WebSocketDisconnect):
|
|
|
+ await self.disconnect(ws)
|
|
|
+ return False
|
|
|
+ except Exception:
|
|
|
+ await self.disconnect(ws)
|
|
|
+ return False
|
|
|
+
|
|
|
+ async def _broadcast(self, connections: Iterable[WebSocket], sender_factory) -> None:
|
|
|
+ dead_connections: list[WebSocket] = []
|
|
|
+ for connection in list(connections):
|
|
|
+ if connection.client_state == WebSocketState.DISCONNECTED:
|
|
|
+ dead_connections.append(connection)
|
|
|
+ continue
|
|
|
+ try:
|
|
|
+ await sender_factory(connection)
|
|
|
+ except (RuntimeError, WebSocketDisconnect):
|
|
|
+ dead_connections.append(connection)
|
|
|
+ except Exception:
|
|
|
+ dead_connections.append(connection)
|
|
|
+ for connection in dead_connections:
|
|
|
+ await self.disconnect(connection)
|
|
|
|
|
|
|
|
|
|