ws
This commit is contained in:
parent
ea84ec400e
commit
00c95cca9e
@ -27,22 +27,18 @@ class WebsocketUtil:
|
|||||||
|
|
||||||
|
|
||||||
def connect(self, ws, id: str):
|
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()
|
msg = ws.receive()
|
||||||
# 存储ws连接对象
|
# 存储ws连接对象
|
||||||
if os.path.exists(f"{id}.pkl"):
|
if os.path.exists(f"{id}.pkl"):
|
||||||
ws_list = read(id=id)
|
ws_list = read(id=id)
|
||||||
ws_list.append(ws)
|
ws_list.append(ws)
|
||||||
write(id=id, ws=ws_list)
|
data = WsFile(id=id, ws=ws_list)
|
||||||
|
write(id=id, ws=data)
|
||||||
else:
|
else:
|
||||||
ws_list = [ws, ]
|
ws_list = [ws, ]
|
||||||
write(id=id, ws=ws_list)
|
data = WsFile(id=id, ws=ws_list)
|
||||||
|
write(id=id, ws=data)
|
||||||
print("--;;-----------", ws_list)
|
print("--;;-----------", ws_list)
|
||||||
|
|
||||||
|
|
||||||
@ -84,7 +80,6 @@ def write(id: str, ws: List):
|
|||||||
print(f"序列化对象{ws}")
|
print(f"序列化对象{ws}")
|
||||||
with open(f"{id}.pkl", "wb") as f:
|
with open(f"{id}.pkl", "wb") as f:
|
||||||
pickle.dump(ws, f)
|
pickle.dump(ws, f)
|
||||||
f.close()
|
|
||||||
|
|
||||||
|
|
||||||
def read(id: str):
|
def read(id: str):
|
||||||
@ -92,17 +87,18 @@ def read(id: str):
|
|||||||
with open(f"{id}.pkl", "rb") as f:
|
with open(f"{id}.pkl", "rb") as f:
|
||||||
wss = pickle.load(f)
|
wss = pickle.load(f)
|
||||||
print(wss)
|
print(wss)
|
||||||
f.close()
|
|
||||||
print(f"反序列化对象{wss}")
|
print(f"反序列化对象{wss}")
|
||||||
return wss
|
return wss
|
||||||
|
|
||||||
|
|
||||||
class Persons():
|
class WsFile:
|
||||||
def __init__(self, x) -> None:
|
def __init__(self, id: str, ws: List) -> None:
|
||||||
self.dict = {
|
self.id = id
|
||||||
"1": 111 * x,
|
self.ws = ws
|
||||||
"2": 222 * x
|
|
||||||
}
|
def __getstate__(self):
|
||||||
|
pickled = {"id": id}
|
||||||
def say(self):
|
return pickled
|
||||||
self.dict[3] = "333"
|
|
||||||
|
def __setstate(self, pickled_dict):
|
||||||
|
self.__init__(pickled_dict['id'])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user