This commit is contained in:
JIAKUNHAO 2022-11-24 15:31:40 +08:00
parent 7f0e578aa3
commit f4afd40961

View File

@ -24,11 +24,13 @@ class WebsocketUtil:
active_connections_dist = {}
self.queue = Queue(maxsize=0)
self.queue.put(active_connections_dist)
print("--------------------队列长度:", self.queue.qsize())
def connect(self, ws, id: str):
# 等待连接
msg = ws.receive()
# 存储ws连接对象
print("--------------------队列长度:", self.queue.qsize())
active_connections_dist = self.queue.get()
if id in active_connections_dist:
active_connections_dist[id].append(ws)
@ -36,16 +38,19 @@ class WebsocketUtil:
ws_list = [ws, ]
active_connections_dist[id] = ws_list
self.queue.put(active_connections_dist)
print("--------------------队列长度:", self.queue.qsize())
def disconnect(self, ws, id):
# ws关闭时 移除ws对象
print("--------------------队列长度:", self.queue.qsize())
if ws.closed:
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
async def send_personal_message(message: str, ws):
@ -74,6 +79,7 @@ class WebsocketUtil:
except Exception as e:
logger.error("websocket异常断开{}", e)
self.disconnect(ws=connection, id=id)
print("--------------------队列长度:", self.queue.qsize())
manager = WebsocketUtil()