diff --git a/app/utils/websocket_tool.py b/app/utils/websocket_tool.py index 53a6d79..18eedd9 100644 --- a/app/utils/websocket_tool.py +++ b/app/utils/websocket_tool.py @@ -27,22 +27,18 @@ class WebsocketUtil: def connect(self, ws, id: str): - p1 = Persons(1) - p2 = Persons(2) - write(id="123", ws=[p1, p2]) - ps = read(id="123") - ps[0].say() - print(ps[1].dict) # 等待连接 msg = ws.receive() # 存储ws连接对象 if os.path.exists(f"{id}.pkl"): ws_list = read(id=id) ws_list.append(ws) - write(id=id, ws=ws_list) + data = WsFile(id=id, ws=ws_list) + write(id=id, ws=data) else: ws_list = [ws, ] - write(id=id, ws=ws_list) + data = WsFile(id=id, ws=ws_list) + write(id=id, ws=data) print("--;;-----------", ws_list) @@ -84,7 +80,6 @@ def write(id: str, ws: List): print(f"序列化对象{ws}") with open(f"{id}.pkl", "wb") as f: pickle.dump(ws, f) - f.close() def read(id: str): @@ -92,17 +87,18 @@ def read(id: str): with open(f"{id}.pkl", "rb") as f: wss = pickle.load(f) print(wss) - f.close() print(f"反序列化对象{wss}") return wss -class Persons(): - def __init__(self, x) -> None: - self.dict = { - "1": 111 * x, - "2": 222 * x - } - - def say(self): - self.dict[3] = "333" +class WsFile: + def __init__(self, id: str, ws: List) -> None: + self.id = id + self.ws = ws + + def __getstate__(self): + pickled = {"id": id} + return pickled + + def __setstate(self, pickled_dict): + self.__init__(pickled_dict['id'])