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