Compare commits
2 Commits
54f4b653be
...
066fb720bc
Author | SHA1 | Date | |
---|---|---|---|
066fb720bc | |||
db888743b7 |
@ -8,8 +8,8 @@
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
import multiprocessing
|
import multiprocessing
|
||||||
global active_connections
|
|
||||||
global active_connections_dist
|
|
||||||
|
|
||||||
def _init(): # 初始化
|
def _init(): # 初始化
|
||||||
# 中断标志
|
# 中断标志
|
||||||
@ -17,9 +17,11 @@ def _init(): # 初始化
|
|||||||
_global_dict = {}
|
_global_dict = {}
|
||||||
|
|
||||||
# ws列表存储
|
# ws列表存储
|
||||||
|
global active_connections
|
||||||
active_connections = multiprocessing.Manager().list()
|
active_connections = multiprocessing.Manager().list()
|
||||||
|
|
||||||
# ws字典存储
|
# ws字典存储
|
||||||
|
global active_connections_dist
|
||||||
active_connections_dist = multiprocessing.Manager().dict()
|
active_connections_dist = multiprocessing.Manager().dict()
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,10 +18,10 @@ class WebsocketUtil:
|
|||||||
# 等待连接
|
# 等待连接
|
||||||
msg = ws.receive()
|
msg = ws.receive()
|
||||||
# 存储ws连接对象
|
# 存储ws连接对象
|
||||||
global active_connections
|
# global active_connections
|
||||||
# active_connections = get_active_connections()
|
# active_connections = get_active_connections()
|
||||||
active_connections.append(ws)
|
active_connections.append(ws)
|
||||||
global active_connections_dist
|
# global active_connections_dist
|
||||||
# active_connections_dist = get_active_connections_dist()
|
# active_connections_dist = get_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)
|
||||||
@ -33,9 +33,9 @@ class WebsocketUtil:
|
|||||||
# ws关闭时 移除ws对象
|
# ws关闭时 移除ws对象
|
||||||
if ws.closed:
|
if ws.closed:
|
||||||
if ws in active_connections_dist.values():
|
if ws in active_connections_dist.values():
|
||||||
global active_connections
|
# global active_connections
|
||||||
active_connections.remove(ws)
|
active_connections.remove(ws)
|
||||||
global active_connections_dist
|
# global active_connections_dist
|
||||||
active_connections_dist[id].pop(ws)
|
active_connections_dist[id].pop(ws)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@ -45,13 +45,13 @@ class WebsocketUtil:
|
|||||||
|
|
||||||
def broadcast(self, message: str):
|
def broadcast(self, message: str):
|
||||||
# 广播消息
|
# 广播消息
|
||||||
global active_connections
|
# global 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
|
||||||
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…
Reference in New Issue
Block a user