This commit is contained in:
xyj 2024-01-03 19:10:56 +08:00
parent ba668a43d1
commit cb25d4da6a
1 changed files with 5 additions and 13 deletions

18
skt.py
View File

@ -97,13 +97,8 @@ def close_stream(username, device):
MQTT.close() MQTT.close()
# 存储所有连接的WebSocket客户端
connected_clients = set()
# 定义函数A # 定义函数A
async def function_A(client, data): async def function_A(client, data):
print(type(data))
try: try:
data = json.loads(data) data = json.loads(data)
except: except:
@ -112,11 +107,12 @@ async def function_A(client, data):
username = data['username'] username = data['username']
device = data['device'] device = data['device']
# return await start(username, device) # return await start(username, device)
return await get()
async def get(): async def get():
return BaseResponse(code=200, msg="success") return BaseResponse(code=200, msg="success")
# 定义函数B # 定义函数B
async def function_B(client, data): async def function_B(client, data):
try: try:
@ -128,12 +124,10 @@ async def function_B(client, data):
device = data['device'] device = data['device']
print(f"{client}的连接已断开") print(f"{client}的连接已断开")
# return await stop(username, device) # return await stop(username, device)
return await get()
# 创建WebSocket连接的处理函数 # 创建WebSocket连接的处理函数
async def handler(websocket, path): async def handler(websocket, path):
# 当新的客户端连接时,添加到集合中
connected_clients.add(websocket)
try: try:
# 在循环中等待客户端的消息 # 在循环中等待客户端的消息
async for message in websocket: async for message in websocket:
@ -144,10 +138,8 @@ async def handler(websocket, path):
print(e) print(e)
await function_B(websocket, message) await function_B(websocket, message)
finally: finally:
# 确保在连接断开时从集合中移除客户端 pass
# print(connected_clients)
connected_clients.remove(websocket)
# await function_B(websocket, message)
# 启动WebSocket服务器 # 启动WebSocket服务器