Files
matagent/backend/consumers.py
2024-12-30 17:01:16 +08:00

25 lines
672 B
Python

# your_app_name/consumers.py
import json
from channels.generic.websocket import AsyncWebsocketConsumer
class ChatConsumer(AsyncWebsocketConsumer):
async def connect(self):
await self.accept()
async def disconnect(self, close_code):
pass
async def receive(self, text_data):
#text_data_json = json.loads(text_data)
#message = text_data_json['message']
# 在这里处理接收到的消息
print(f"Received message: {text_data}")
# 发送消息回客户端
response_text = {
'message': f'You said: {text_data}'
}
await self.send(text_data=json.dumps(response_text))