冲突解决
This commit is contained in:
@ -1,8 +1,6 @@
|
||||
"""
|
||||
@Time : 2022/10/12 17:55
|
||||
@Auth : 东
|
||||
@File :websocket_tool.py
|
||||
@IDE :PyCharm
|
||||
@Motto:ABC(Always Be Coding)
|
||||
@Desc:
|
||||
|
||||
@ -10,32 +8,35 @@
|
||||
import json
|
||||
from typing import Union, List, Dict
|
||||
|
||||
from core.common_utils import logger
|
||||
from utils.JSONEncodeTools import MyEncoder
|
||||
|
||||
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 +45,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:
|
||||
|
Reference in New Issue
Block a user