ws
This commit is contained in:
parent
7f0e578aa3
commit
f4afd40961
@ -24,11 +24,13 @@ class WebsocketUtil:
|
|||||||
active_connections_dist = {}
|
active_connections_dist = {}
|
||||||
self.queue = Queue(maxsize=0)
|
self.queue = Queue(maxsize=0)
|
||||||
self.queue.put(active_connections_dist)
|
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())
|
||||||
active_connections_dist = self.queue.get()
|
active_connections_dist = self.queue.get()
|
||||||
if id in active_connections_dist:
|
if id in active_connections_dist:
|
||||||
active_connections_dist[id].append(ws)
|
active_connections_dist[id].append(ws)
|
||||||
@ -36,16 +38,19 @@ class WebsocketUtil:
|
|||||||
ws_list = [ws, ]
|
ws_list = [ws, ]
|
||||||
active_connections_dist[id] = ws_list
|
active_connections_dist[id] = ws_list
|
||||||
self.queue.put(active_connections_dist)
|
self.queue.put(active_connections_dist)
|
||||||
|
print("--------------------队列长度:", self.queue.qsize())
|
||||||
|
|
||||||
|
|
||||||
def disconnect(self, ws, id):
|
def disconnect(self, ws, id):
|
||||||
# ws关闭时 移除ws对象
|
# ws关闭时 移除ws对象
|
||||||
|
print("--------------------队列长度:", self.queue.qsize())
|
||||||
if ws.closed:
|
if ws.closed:
|
||||||
active_connections_dist = self.queue.get()
|
active_connections_dist = self.queue.get()
|
||||||
if active_connections_dist is not None:
|
if active_connections_dist is not None:
|
||||||
if ws in active_connections_dist.values():
|
if ws in active_connections_dist.values():
|
||||||
active_connections_dist[id].pop(ws)
|
active_connections_dist[id].pop(ws)
|
||||||
self.queue.put(active_connections_dist)
|
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):
|
||||||
@ -74,6 +79,7 @@ class WebsocketUtil:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("websocket异常断开,{}", e)
|
logger.error("websocket异常断开,{}", e)
|
||||||
self.disconnect(ws=connection, id=id)
|
self.disconnect(ws=connection, id=id)
|
||||||
|
print("--------------------队列长度:", self.queue.qsize())
|
||||||
|
|
||||||
|
|
||||||
manager = WebsocketUtil()
|
manager = WebsocketUtil()
|
Loading…
Reference in New Issue
Block a user