ws
This commit is contained in:
parent
f4afd40961
commit
dd74564f2e
@ -16,20 +16,20 @@ def _init(): # 初始化
|
|||||||
global _global_dict
|
global _global_dict
|
||||||
_global_dict = {}
|
_global_dict = {}
|
||||||
|
|
||||||
# ws列表存储
|
# # ws列表存储
|
||||||
global _active_connections
|
# global _active_connections
|
||||||
_active_connections = multiprocessing.Manager().list()
|
# _active_connections = multiprocessing.Manager().list()
|
||||||
|
|
||||||
# ws字典存储
|
# # ws字典存储
|
||||||
global _active_connections_dist
|
# global _active_connections_dist
|
||||||
_active_connections_dist = multiprocessing.Manager().dict()
|
# _active_connections_dist = multiprocessing.Manager().dict()
|
||||||
|
|
||||||
|
|
||||||
def get_active_connections():
|
# def get_active_connections():
|
||||||
return _active_connections
|
# return _active_connections
|
||||||
|
|
||||||
def get_active_connections_dist():
|
# def get_active_connections_dist():
|
||||||
return _active_connections_dist
|
# return _active_connections_dist
|
||||||
|
|
||||||
def set_value(key, value):
|
def set_value(key, value):
|
||||||
# 定义一个全局变量
|
# 定义一个全局变量
|
||||||
|
@ -9,48 +9,40 @@
|
|||||||
"""
|
"""
|
||||||
import json
|
import json
|
||||||
from typing import Union, List, Dict
|
from typing import Union, List, Dict
|
||||||
|
import os
|
||||||
|
|
||||||
from app.core.common_utils import logger
|
from app.core.common_utils import logger
|
||||||
from app.utils.JSONEncodeTools import MyEncoder
|
from app.utils.JSONEncodeTools import MyEncoder
|
||||||
from configs.global_var import *
|
from configs.global_var import *
|
||||||
from app.utils.redis_config import redis_client
|
from app.utils.redis_config import redis_client
|
||||||
from multiprocessing import Process, Queue
|
from multiprocessing import Process, Queue
|
||||||
|
import pickle
|
||||||
|
|
||||||
|
|
||||||
class WebsocketUtil:
|
class WebsocketUtil:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# self.active_connections = multiprocessing.Manager().list()
|
# self.active_connections = multiprocessing.Manager().list()
|
||||||
active_connections_dist = {}
|
self.active_connections_dist = {}
|
||||||
self.queue = Queue(maxsize=0)
|
|
||||||
self.queue.put(active_connections_dist)
|
|
||||||
print("--------------------队列长度:", self.queue.qsize())
|
|
||||||
|
|
||||||
def connect(self, ws, id: str):
|
def connect(self, ws, id: str):
|
||||||
# 等待连接
|
# 等待连接
|
||||||
msg = ws.receive()
|
msg = ws.receive()
|
||||||
# 存储ws连接对象
|
# 存储ws连接对象
|
||||||
print("--------------------队列长度:", self.queue.qsize())
|
if os.path.exists(f"{id}.pkl"):
|
||||||
active_connections_dist = self.queue.get()
|
ws_list = read(id=id)
|
||||||
if id in active_connections_dist:
|
ws_list.append(ws)
|
||||||
active_connections_dist[id].append(ws)
|
|
||||||
else:
|
else:
|
||||||
ws_list = [ws, ]
|
ws_list = [ws, ]
|
||||||
active_connections_dist[id] = ws_list
|
write(id=id, ws=list)
|
||||||
self.queue.put(active_connections_dist)
|
|
||||||
print("--------------------队列长度:", self.queue.qsize())
|
|
||||||
|
|
||||||
|
|
||||||
def disconnect(self, ws, id):
|
def disconnect(self, ws, id):
|
||||||
# ws关闭时 移除ws对象
|
# 删除连接
|
||||||
print("--------------------队列长度:", self.queue.qsize())
|
if os.path.exists(f"{id}.pkl"):
|
||||||
if ws.closed:
|
os.remove(f"{id}.pkl")
|
||||||
active_connections_dist = self.queue.get()
|
|
||||||
if active_connections_dist is not None:
|
|
||||||
if ws in active_connections_dist.values():
|
|
||||||
active_connections_dist[id].pop(ws)
|
|
||||||
self.queue.put(active_connections_dist)
|
|
||||||
print("--------------------队列长度:", self.queue.qsize())
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def send_personal_message(message: str, ws):
|
async def send_personal_message(message: str, ws):
|
||||||
@ -68,18 +60,25 @@ class WebsocketUtil:
|
|||||||
|
|
||||||
def send_message_proj_json(self, message: Union[str, int, List, Dict], id: str):
|
def send_message_proj_json(self, message: Union[str, int, List, Dict], id: str):
|
||||||
# 广播该项目的消息
|
# 广播该项目的消息
|
||||||
# global active_connections_dist
|
active_connections_dist = read(id="{id}.pkl")
|
||||||
print(self.queue.qsize())
|
|
||||||
active_connections_dist = self.queue.get()
|
|
||||||
self.queue.put(active_connections_dist)
|
|
||||||
print(active_connections_dist)
|
|
||||||
for connection in active_connections_dist[id]:
|
for connection in active_connections_dist[id]:
|
||||||
try:
|
try:
|
||||||
connection.send(json.dumps(message, cls=MyEncoder, indent=4), )
|
connection.send(json.dumps(message, cls=MyEncoder, indent=4), )
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("websocket异常断开,{}", e)
|
logger.error("websocket异常断开,{}", e)
|
||||||
self.disconnect(ws=connection, id=id)
|
|
||||||
print("--------------------队列长度:", self.queue.qsize())
|
|
||||||
|
|
||||||
|
|
||||||
manager = WebsocketUtil()
|
manager = WebsocketUtil()
|
||||||
|
|
||||||
|
|
||||||
|
def write(id: str, ws: List):
|
||||||
|
with open(f"{id}.pkl", "wb") as f:
|
||||||
|
wsdump = pickle.dump(ws)
|
||||||
|
f.write(wsdump)
|
||||||
|
|
||||||
|
|
||||||
|
def read(id: str):
|
||||||
|
with open(f"{id}.pkl", "rb") as f:
|
||||||
|
wss = pickle.load(f)
|
||||||
|
print(wss)
|
||||||
|
return wss
|
Loading…
Reference in New Issue
Block a user