2022-11-04 17:37:08 +08:00
|
|
|
|
"""
|
|
|
|
|
@Time : 2022/10/12 17:55
|
|
|
|
|
@Auth : 东
|
2022-11-24 10:58:58 +08:00
|
|
|
|
@File :websocket_tool.py
|
|
|
|
|
@IDE :PyCharm
|
2022-11-04 17:37:08 +08:00
|
|
|
|
@Motto:ABC(Always Be Coding)
|
|
|
|
|
@Desc:
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
import json
|
|
|
|
|
from typing import Union, List, Dict
|
2022-11-24 16:45:04 +08:00
|
|
|
|
import os
|
2022-11-04 17:37:08 +08:00
|
|
|
|
|
|
|
|
|
from app.core.common_utils import logger
|
|
|
|
|
from app.utils.JSONEncodeTools import MyEncoder
|
2022-11-24 10:45:05 +08:00
|
|
|
|
from configs.global_var import *
|
2022-11-24 14:15:23 +08:00
|
|
|
|
from app.utils.redis_config import redis_client
|
2022-11-24 15:21:00 +08:00
|
|
|
|
from multiprocessing import Process, Queue
|
2022-11-24 16:45:04 +08:00
|
|
|
|
import pickle
|
2022-11-04 17:37:08 +08:00
|
|
|
|
|
|
|
|
|
|
2022-11-24 11:15:07 +08:00
|
|
|
|
class WebsocketUtil:
|
|
|
|
|
|
2022-11-24 15:21:00 +08:00
|
|
|
|
def __init__(self):
|
|
|
|
|
# self.active_connections = multiprocessing.Manager().list()
|
2022-11-24 17:04:46 +08:00
|
|
|
|
self.active_connections_dist = []
|
2022-11-24 16:45:04 +08:00
|
|
|
|
|
2022-11-24 10:58:58 +08:00
|
|
|
|
|
2022-11-04 17:37:08 +08:00
|
|
|
|
def connect(self, ws, id: str):
|
|
|
|
|
# 等待连接
|
|
|
|
|
msg = ws.receive()
|
|
|
|
|
# 存储ws连接对象
|
2022-11-24 16:45:04 +08:00
|
|
|
|
if os.path.exists(f"{id}.pkl"):
|
2022-11-24 17:40:35 +08:00
|
|
|
|
wsFile = read(id=id)
|
|
|
|
|
ws_list = wsFile.ws
|
2022-11-24 16:45:04 +08:00
|
|
|
|
ws_list.append(ws)
|
2022-11-24 17:36:54 +08:00
|
|
|
|
data = WsFile(id=id, ws=ws_list)
|
|
|
|
|
write(id=id, ws=data)
|
2022-11-04 17:37:08 +08:00
|
|
|
|
else:
|
|
|
|
|
ws_list = [ws, ]
|
2022-11-24 17:36:54 +08:00
|
|
|
|
data = WsFile(id=id, ws=ws_list)
|
|
|
|
|
write(id=id, ws=data)
|
2022-11-24 17:06:21 +08:00
|
|
|
|
print("--;;-----------", ws_list)
|
2022-11-24 16:45:04 +08:00
|
|
|
|
|
2022-11-24 14:15:23 +08:00
|
|
|
|
|
2022-11-04 17:37:08 +08:00
|
|
|
|
|
|
|
|
|
def disconnect(self, ws, id):
|
2022-11-24 16:45:04 +08:00
|
|
|
|
# 删除连接
|
|
|
|
|
if os.path.exists(f"{id}.pkl"):
|
|
|
|
|
os.remove(f"{id}.pkl")
|
2022-11-04 17:37:08 +08:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
async def send_personal_message(message: str, ws):
|
|
|
|
|
# 发送个人消息
|
|
|
|
|
await ws.send(message)
|
|
|
|
|
|
2022-11-24 15:21:00 +08:00
|
|
|
|
# def broadcast(self, message: str):
|
|
|
|
|
# # 广播消息
|
|
|
|
|
# # global active_connections
|
|
|
|
|
# active_connections = redis_client.get_redis().get("active_connections")
|
|
|
|
|
# if active_connections is not None:
|
|
|
|
|
# active_connections = json.loads(active_connections)
|
|
|
|
|
# for connection in active_connections:
|
|
|
|
|
# connection.send(message)
|
2022-11-04 17:37:08 +08:00
|
|
|
|
|
|
|
|
|
def send_message_proj_json(self, message: Union[str, int, List, Dict], id: str):
|
|
|
|
|
# 广播该项目的消息
|
2022-11-24 16:52:54 +08:00
|
|
|
|
active_connections = read(id=id)
|
2022-11-24 17:44:14 +08:00
|
|
|
|
print("===================", type(active_connections.id), active_connections.id)
|
2022-11-24 17:40:35 +08:00
|
|
|
|
print("===================", type(active_connections.ws), active_connections.ws[0])
|
|
|
|
|
for connection in active_connections.ws:
|
2022-11-24 15:21:00 +08:00
|
|
|
|
try:
|
|
|
|
|
connection.send(json.dumps(message, cls=MyEncoder, indent=4), )
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error("websocket异常断开,{}", e)
|
2022-11-04 17:37:08 +08:00
|
|
|
|
|
|
|
|
|
|
2022-11-24 16:45:04 +08:00
|
|
|
|
manager = WebsocketUtil()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def write(id: str, ws: List):
|
2022-11-24 17:12:46 +08:00
|
|
|
|
print(f"序列化对象{ws}")
|
2022-11-24 16:45:04 +08:00
|
|
|
|
with open(f"{id}.pkl", "wb") as f:
|
2022-11-24 17:21:42 +08:00
|
|
|
|
pickle.dump(ws, f)
|
2022-11-24 16:45:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def read(id: str):
|
2022-11-24 17:12:46 +08:00
|
|
|
|
|
2022-11-24 16:45:04 +08:00
|
|
|
|
with open(f"{id}.pkl", "rb") as f:
|
|
|
|
|
wss = pickle.load(f)
|
|
|
|
|
print(wss)
|
2022-11-24 17:12:46 +08:00
|
|
|
|
print(f"反序列化对象{wss}")
|
2022-11-24 17:24:11 +08:00
|
|
|
|
return wss
|
|
|
|
|
|
|
|
|
|
|
2022-11-24 17:36:54 +08:00
|
|
|
|
class WsFile:
|
|
|
|
|
def __init__(self, id: str, ws: List) -> None:
|
|
|
|
|
self.id = id
|
|
|
|
|
self.ws = ws
|
|
|
|
|
|
|
|
|
|
def __getstate__(self):
|
2022-11-24 17:44:14 +08:00
|
|
|
|
pickled = {"id": self.id}
|
2022-11-24 17:36:54 +08:00
|
|
|
|
return pickled
|
|
|
|
|
|
2022-11-24 17:46:19 +08:00
|
|
|
|
def getWs(self):
|
|
|
|
|
return self.ws
|
|
|
|
|
|
2022-11-24 17:36:54 +08:00
|
|
|
|
def __setstate(self, pickled_dict):
|
|
|
|
|
self.__init__(pickled_dict['id'])
|