From b31867f12abb88e2ef18fdedaea8cf68a29e0234 Mon Sep 17 00:00:00 2001 From: JIAKUNHAO Date: Thu, 24 Nov 2022 11:18:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/utils/websocket_tool.py | 44 ++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/app/utils/websocket_tool.py b/app/utils/websocket_tool.py index ad4db99..0b31cf3 100644 --- a/app/utils/websocket_tool.py +++ b/app/utils/websocket_tool.py @@ -14,39 +14,39 @@ from app.core.common_utils import logger from app.utils.JSONEncodeTools import MyEncoder from configs.global_var import * -# global active_connections -# active_connections = multiprocessing.Manager().list() +global active_connections +active_connections = multiprocessing.Manager().list() -# global active_connections_dist -# active_connections_dist = multiprocessing.Manager().dict() +global active_connections_dist +active_connections_dist = multiprocessing.Manager().dict() class WebsocketUtil: - def __init__(self): - self.active_connections = multiprocessing.Manager().list() - self.active_connections_dist = multiprocessing.Manager().dict() + # def __init__(self): + # self.active_connections = multiprocessing.Manager().list() + # self.active_connections_dist = multiprocessing.Manager().dict() def connect(self, ws, id: str): # 等待连接 msg = ws.receive() # 存储ws连接对象 - # global active_connections - self.active_connections.append(ws) - # global active_connections_dist - if id in self.active_connections_dist: - self.active_connections_dist[id].append(ws) + global active_connections + active_connections.append(ws) + global active_connections_dist + if id in active_connections_dist: + active_connections_dist[id].append(ws) else: ws_list = [ws, ] - self.active_connections_dist[id] = ws_list + active_connections_dist[id] = ws_list def disconnect(self, ws, id): # ws关闭时 移除ws对象 if ws.closed: - if ws in self.active_connections_dist.values(): - # global active_connections - self.active_connections.remove(ws) - # global active_connections_dist - self.active_connections_dist[id].pop(ws) + if ws in active_connections_dist.values(): + global active_connections + active_connections.remove(ws) + global active_connections_dist + active_connections_dist[id].pop(ws) @staticmethod async def send_personal_message(message: str, ws): @@ -55,14 +55,14 @@ class WebsocketUtil: def broadcast(self, message: str): # 广播消息 - # global active_connections - for connection in self.active_connections: + global active_connections + for connection in active_connections: connection.send(message) def send_message_proj_json(self, message: Union[str, int, List, Dict], id: str): # 广播该项目的消息 - # global active_connections_dist - for connection in self.active_connections_dist[id]: + global active_connections_dist + for connection in active_connections_dist[id]: try: connection.send(json.dumps(message, cls=MyEncoder, indent=4), ) except Exception as e: