From 27284596362ba623a6486ac752af6f403f3e728f Mon Sep 17 00:00:00 2001 From: JIAKUNHAO Date: Thu, 24 Nov 2022 10:45:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=A8=E5=B1=80ws-2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/configs/global_var.py | 11 ++++++++--- app/utils/websocket_tool.py | 31 ++++++++++++++++++------------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/app/configs/global_var.py b/app/configs/global_var.py index 391316f..2a2dfe0 100644 --- a/app/configs/global_var.py +++ b/app/configs/global_var.py @@ -8,7 +8,8 @@ """ import multiprocessing - +global active_connections +global active_connections_dist def _init(): # 初始化 # 中断标志 @@ -16,14 +17,18 @@ def _init(): # 初始化 _global_dict = {} # ws列表存储 - global active_connections active_connections = multiprocessing.Manager().list() # ws字典存储 - global active_connections_dist active_connections_dist = multiprocessing.Manager().dict() +def get_active_connections(): + return active_connections + +def get_active_connections_dist(): + return active_connections_dist + def set_value(key, value): # 定义一个全局变量 _global_dict[key] = value diff --git a/app/utils/websocket_tool.py b/app/utils/websocket_tool.py index e69defd..e8863cb 100644 --- a/app/utils/websocket_tool.py +++ b/app/utils/websocket_tool.py @@ -12,30 +12,33 @@ from typing import Union, List, Dict from app.core.common_utils import logger from app.utils.JSONEncodeTools import MyEncoder - +from configs.global_var import * class WebsocketUtil: - def __init__(self): - self.active_connections: List = [] - self.active_connections_dist: Dict = {} def connect(self, ws, id: str): # 等待连接 msg = ws.receive() # 存储ws连接对象 - self.active_connections.append(ws) - if id in self.active_connections_dist: - self.active_connections_dist[id].append(ws) + global active_connections + # active_connections = get_active_connections() + active_connections.append(ws) + global active_connections_dist + # active_connections_dist = get_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(): - self.active_connections.remove(ws) - 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): @@ -44,12 +47,14 @@ class WebsocketUtil: def broadcast(self, message: str): # 广播消息 - 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): # 广播该项目的消息 - 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: