Django在模型更改时传递消息



我正在使用django通道在后端组织我的websocket。现在,除了数据库中的信息发生更改时向前端发送消息外,一切都正常。有http端点可以更改模型。

这是我的websocket消费者

import asyncio
from asgiref.sync import async_to_sync, sync_to_async
from channels.generic.websocket import AsyncJsonWebsocketConsumer
from rest_framework.response import Response
from rest_framework import status
from channels.db import database_sync_to_async
# models and serializers
from billing.models import Billing
from billing.serializers import BillingSerializer
from customers.models import Customer
from customers.serializers import CustomersSerializer
from shipping.models import Shipping
from shipping.serializers import ShippingSerializer
from .models import Order
from .serializers import OrdersSerializer
from .views import OrdersViewSet
from .exceptions import ClientError
from .utils import get_orders_or_error, get_orders_or_warning

class OrdersConsumer(AsyncJsonWebsocketConsumer):
async def connect(self):
orders = await get_orders_or_warning()
if 'user' in self.scope:
await self.close()
else:
await self.accept()
self.orders = set(orders)
# await self.create_order(content)
async def receive_json(self, content):
command = content.get('command', None)
orders = await get_orders_or_warning()
# print(list(self.orders))
# print(set(orders))
try:
if command == "join":
await self.join_room(JWT_Token=content['token'])
elif command == "leave":
await self.leave_room()
elif command == "send":
await self.send_room(content['message'])
except ClientError as e:
await self.send_json({"error": e.code})
async def disconnect(self, code):
for room_id in list(self.rooms):
try:
self.send_json({
'type': 'CLOSE',
'message': "Socket closed"
})
await self.leave_room(content['token'])
except ClientError:
pass
async def join_room(self, JWT_Token):
orders = await get_orders_or_warning()
serializer = OrdersSerializer(orders, many=True)
# print('serializer', serializer)
if self.orders != set(orders):
await self.send_json(
{
'type': 'orders',
'data': json.dumps(serializer.data),
},
)
await self.send_json(
{
'type': 'orders',
'data': json.dumps(serializer.data),
},
)
async def leave_room(self, JWT_Token):
await self.channel_layer.group_send(
orders.group_name,
{
'type': 'orders.leave',
'JWT_Token': JWT_Token
}
)
self.rooms.discard()
await self.channel_layer.group_discard(
orders.group_name,
self.channel_name
)
await self.send_json({
"leave": str(room_id),
})
async def send_room(self, message):
if room_id not in self.rooms:
raise ClientError("ROOM_ACCESS_DENIED")
orders = await get_orders_or_warning()
serializer = OrdersSerializer(orders, many=True)
await self.send_json(
{
'type': 'orders.give',
'data': json.dumps(serializer.data)
}
)
await self.send(text_data=json.dumps({
'message': message
}))
async def orders_leave(self, event):
await self.send_json(
{
'type': 'LEAVE',
'room': event["room_id"],
'username': event["username"]
}
)

这是我的路由文件


application = ProtocolTypeRouter({
'websocket': (
URLRouter([
url('orders/', OrdersConsumer),
])
)
})

我想在发生一些更改时使用前端获取所有数据。可以对当前消费者进行此操作吗?如果是,怎么做?我想我已经看了太多的信息来源,现在我有点困惑我怎么才能真正做到这一点。

如果可能的话,我不想重写结构。如果你能解释我需要如何以及为什么按照你说的方式写,我将非常感谢

您可以使用信号来监控更改,并使用文档中定义的方法向消费者发送消息,说明如何从外部与消费者交互

相关内容

  • 没有找到相关文章

最新更新