Django 频道实时聊天保存发送的消息
Django channels live chat save sent messages
所以我有一个 Django 应用程序,我在其中使用频道来实现实时聊天。我的消费者看起来像这样:
进口json
从 asgiref.sync 导入 async_to_sync
从 channels.generic.websocket 导入 WebsocketConsumer
class ChatConsumer(WebsocketConsumer):
def connect(self):
self.room_name = self.scope['url_route']['kwargs']['room_name']
self.room_group_name = 'chat_%s' % self.room_name
# Join room group
async_to_sync(self.channel_layer.group_add)(
self.room_group_name,
self.channel_name
)
self.accept()
def disconnect(self, close_code):
# Leave room group
async_to_sync(self.channel_layer.group_discard)(
self.room_group_name,
self.channel_name
)
# Receive message from WebSocket
def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json['message']
username = self.scope["user"]
# Send message to room group
async_to_sync(self.channel_layer.group_send)(
self.room_group_name,
{
'type': 'chat_message',
'message': message,
'user': username.username
}
)
# Receive message from room group
def chat_message(self, event):
message = event['message']
user=event['user']
print(user)
# Send message to WebSocket
self.send(text_data=json.dumps({
'message': message,
'user':user
}))
所以我正在寻找一种方法来保存已发送的消息(因为目前它们在刷新时丢失了)。我创建了一个 Messages 模型,该模型具有用于消息文本的 CharField。如果我在我的 chat_message 函数中这样做,我想我可以保存新消息:
new_message=Messages(text=message)
new_nessage.save()
我的问题是如何在用户连接到聊天时预加载最后 10 条消息?
所以为了做到这一点,我最终在“接收”功能中将我的消息保存到数据库中(因为如果你将它保存在 chat_message 中,它会为每个活跃用户保存 1 次) .然后,为了预加载消息,每次打开 websocket 时我都使用 AJAX 调用,以便使用 python 函数获取 10 条最新消息,然后我将消息作为 JsonResponse (https://simpleisbetterthancomplex.com/tutorial/2016/08/29/how-to-work-with-ajax-request-with-django.html 向下滚动到本文的 AJAX 请求部分,并按此顺序查看 signup.html、urls.py 和 views.py 文件以供参考)。
所以我有一个 Django 应用程序,我在其中使用频道来实现实时聊天。我的消费者看起来像这样:
进口json 从 asgiref.sync 导入 async_to_sync 从 channels.generic.websocket 导入 WebsocketConsumer
class ChatConsumer(WebsocketConsumer):
def connect(self):
self.room_name = self.scope['url_route']['kwargs']['room_name']
self.room_group_name = 'chat_%s' % self.room_name
# Join room group
async_to_sync(self.channel_layer.group_add)(
self.room_group_name,
self.channel_name
)
self.accept()
def disconnect(self, close_code):
# Leave room group
async_to_sync(self.channel_layer.group_discard)(
self.room_group_name,
self.channel_name
)
# Receive message from WebSocket
def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json['message']
username = self.scope["user"]
# Send message to room group
async_to_sync(self.channel_layer.group_send)(
self.room_group_name,
{
'type': 'chat_message',
'message': message,
'user': username.username
}
)
# Receive message from room group
def chat_message(self, event):
message = event['message']
user=event['user']
print(user)
# Send message to WebSocket
self.send(text_data=json.dumps({
'message': message,
'user':user
}))
所以我正在寻找一种方法来保存已发送的消息(因为目前它们在刷新时丢失了)。我创建了一个 Messages 模型,该模型具有用于消息文本的 CharField。如果我在我的 chat_message 函数中这样做,我想我可以保存新消息:
new_message=Messages(text=message)
new_nessage.save()
我的问题是如何在用户连接到聊天时预加载最后 10 条消息?
所以为了做到这一点,我最终在“接收”功能中将我的消息保存到数据库中(因为如果你将它保存在 chat_message 中,它会为每个活跃用户保存 1 次) .然后,为了预加载消息,每次打开 websocket 时我都使用 AJAX 调用,以便使用 python 函数获取 10 条最新消息,然后我将消息作为 JsonResponse (https://simpleisbetterthancomplex.com/tutorial/2016/08/29/how-to-work-with-ajax-request-with-django.html 向下滚动到本文的 AJAX 请求部分,并按此顺序查看 signup.html、urls.py 和 views.py 文件以供参考)。