ws
This commit is contained in:
parent
4842024f99
commit
fcb39c8863
@ -14,53 +14,38 @@ from app.core.common_utils import logger
|
|||||||
from app.utils.JSONEncodeTools import MyEncoder
|
from app.utils.JSONEncodeTools import MyEncoder
|
||||||
from configs.global_var import *
|
from configs.global_var import *
|
||||||
from app.utils.redis_config import redis_client
|
from app.utils.redis_config import redis_client
|
||||||
|
from multiprocessing import Process, Queue
|
||||||
|
|
||||||
|
|
||||||
class WebsocketUtil:
|
class WebsocketUtil:
|
||||||
|
|
||||||
# def __init__(self):
|
def __init__(self):
|
||||||
# self.active_connections = multiprocessing.Manager().list()
|
# self.active_connections = multiprocessing.Manager().list()
|
||||||
# self.active_connections_dist = multiprocessing.Manager().dict()
|
active_connections_dist = {}
|
||||||
|
self.queue = Queue(maxsize=0)
|
||||||
|
self.queue.put(active_connections_dist)
|
||||||
|
|
||||||
def connect(self, ws, id: str):
|
def connect(self, ws, id: str):
|
||||||
# 等待连接
|
# 等待连接
|
||||||
msg = ws.receive()
|
msg = ws.receive()
|
||||||
# 存储ws连接对象
|
# 存储ws连接对象
|
||||||
active_connections = redis_client.get_redis().get("active_connections")
|
active_connections_dist = Queue.get()
|
||||||
if active_connections == None:
|
|
||||||
active_connections = []
|
|
||||||
else:
|
|
||||||
active_connections = json.loads(active_connections)
|
|
||||||
active_connections.append(ws)
|
|
||||||
active_connections_dist = redis_client.get_redis().get("active_connections_dist")
|
|
||||||
if active_connections_dist == None:
|
|
||||||
active_connections_dist = {}
|
|
||||||
else:
|
|
||||||
active_connections_dist = json.loads(active_connections_dist)
|
|
||||||
if id in active_connections_dist:
|
if id in active_connections_dist:
|
||||||
active_connections_dist[id].append(ws)
|
active_connections_dist[id].append(ws)
|
||||||
else:
|
else:
|
||||||
ws_list = [ws, ]
|
ws_list = [ws, ]
|
||||||
active_connections_dist[id] = ws_list
|
active_connections_dist[id] = ws_list
|
||||||
redis_client.get_redis().set("active_connections", json.dumps(active_connections))
|
self.queue.put(active_connections_dist)
|
||||||
redis_client.get_redis().set("active_connections_dist", json.dumps(active_connections_dist))
|
|
||||||
|
|
||||||
|
|
||||||
def disconnect(self, ws, id):
|
def disconnect(self, ws, id):
|
||||||
# ws关闭时 移除ws对象
|
# ws关闭时 移除ws对象
|
||||||
if ws.closed:
|
if ws.closed:
|
||||||
active_connections_dist = redis_client.get_redis().get("active_connections_dist")
|
active_connections_dist = Queue.get()
|
||||||
if active_connections_dist is not None:
|
if active_connections_dist is not None:
|
||||||
active_connections_dist = json.loads(active_connections_dist)
|
|
||||||
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)
|
||||||
redis_client.get_redis().set("active_connections", json.dumps(active_connections))
|
redis_client.get_redis().set("active_connections", json.dumps(active_connections))
|
||||||
active_connections = redis_client.get_redis().get("active_connections")
|
|
||||||
if active_connections is not None:
|
|
||||||
active_connections = json.loads(active_connections)
|
|
||||||
if ws in active_connections.values():
|
|
||||||
active_connections.remove(ws)
|
|
||||||
redis_client.get_redis().set("active_connections_dist", json.dumps(active_connections_dist))
|
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@ -68,21 +53,20 @@ class WebsocketUtil:
|
|||||||
# 发送个人消息
|
# 发送个人消息
|
||||||
await ws.send(message)
|
await ws.send(message)
|
||||||
|
|
||||||
def broadcast(self, message: str):
|
# def broadcast(self, message: str):
|
||||||
# 广播消息
|
# # 广播消息
|
||||||
# global active_connections
|
# # global active_connections
|
||||||
active_connections = redis_client.get_redis().get("active_connections")
|
# active_connections = redis_client.get_redis().get("active_connections")
|
||||||
if active_connections is not None:
|
# if active_connections is not None:
|
||||||
active_connections = json.loads(active_connections)
|
# active_connections = json.loads(active_connections)
|
||||||
for connection in active_connections:
|
# for connection in active_connections:
|
||||||
connection.send(message)
|
# connection.send(message)
|
||||||
|
|
||||||
def send_message_proj_json(self, message: Union[str, int, List, Dict], id: str):
|
def send_message_proj_json(self, message: Union[str, int, List, Dict], id: str):
|
||||||
# 广播该项目的消息
|
# 广播该项目的消息
|
||||||
# global active_connections_dist
|
# global active_connections_dist
|
||||||
active_connections_dist = redis_client.get_redis().get("active_connections_dist")
|
active_connections_dist = self.queue.get()
|
||||||
if active_connections_dist is not None:
|
self.queue.put(active_connections_dist)
|
||||||
active_connections_dist = json.loads(active_connections_dist)
|
|
||||||
for connection in active_connections_dist[id]:
|
for connection in active_connections_dist[id]:
|
||||||
try:
|
try:
|
||||||
connection.send(json.dumps(message, cls=MyEncoder, indent=4), )
|
connection.send(json.dumps(message, cls=MyEncoder, indent=4), )
|
||||||
|
Loading…
x
Reference in New Issue
Block a user