27 lines
935 B
Python
27 lines
935 B
Python
"""智能终端状态 WebSocket 消费者 — 向前端推送终端上下线状态变化"""
|
|
import json
|
|
|
|
from channels.generic.websocket import AsyncWebsocketConsumer
|
|
|
|
|
|
class TerminalStatusConsumer(AsyncWebsocketConsumer):
|
|
"""终端状态消费者
|
|
|
|
WebSocket 路径: ws://host/ws/terminal_status/
|
|
Channel Group: terminal_status
|
|
消息类型: terminal.status
|
|
"""
|
|
|
|
async def connect(self):
|
|
# 加入 terminal_status 广播组
|
|
await self.channel_layer.group_add("terminal_status", self.channel_name)
|
|
await self.accept()
|
|
|
|
async def disconnect(self, close_code):
|
|
# 离开 terminal_status 广播组
|
|
await self.channel_layer.group_discard("terminal_status", self.channel_name)
|
|
|
|
async def terminal_status(self, event):
|
|
"""处理终端状态变化事件,转发给前端"""
|
|
await self.send(text_data=json.dumps(event["data"], ensure_ascii=False))
|