我正在使用異步編程開發帶有通道和 websocket 的聊天應用程式。我無法在 consumer.py 中獲取模型資料/物件,但能夠創建一個。
當組中的某個人發送訊息時,它會回顯給整個組,但不會保存,因此會在頁面重繪 后重繪 。我想將訊息保存在資料庫中,因為訊息是使用 websockets 發送到組的。但我面臨問題。
這是我的消費者.py
import json
from channels.generic.websocket import AsyncWebsocketConsumer
from asgiref.sync import sync_to_async
from django.contrib.auth.models import User
from chat.models import ChatMessage , ChatRoom
from channels.db import database_sync_to_async
class ChatConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.roomId = self.scope['url_route']['kwargs']['roomId']
self.room_group_name = 'chat_group_%s' % self.roomId
await self.channel_layer.group_add(
self.room_group_name,
self.channel_name
)
await self.accept()
async def disconnect(self , close_code):
await self.channel_layer.group_discard(
self.room_group_name,
self.channel_name
)
async def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json["message"]
username = text_data_json["username"]
roomId = text_data_json["roomId"]
roomName = text_data_json["roomName"]
await self.save_message(message , username , roomId , roomName)
await self.channel_layer.group_send(
self.room_group_name,
{
'type': 'the_message',
'message': message
}
)
async def the_message(self, event):
message = event['message']
await self.send(text_data=json.dumps({
'message': message
}))
@sync_to_async
def save_message(self , message , username , roomId , roomName) :
user = User.objects.get(username = username)
the_room = ChatRoom.objects.get(roomname = roomName , id = roomId)
new_message = ChatMessage(user = user , chatroom = the_room , message = message )
new_message.save()
這是我的models.py
from django.db import models
from django.contrib.auth.models import User
from datetime import datetime
class ChatRoom(models.Model) :
host = models.ForeignKey(User , on_delete = models.CASCADE)
roomname = models.CharField(max_length = 100 , blank = False , null = False)
participants = models.ManyToManyField(User , verbose_name = "participants" , related_name = "participants")
created = models.DateTimeField(auto_now_add = True)
updated = models.DateTimeField(auto_now = True)
class Meta:
ordering = ["-updated" , "-created"]
def __str__(self) :
return str(self.host) " created " str(self.roomname)
class ChatMessage(models.Model) :
user = models.ForeignKey(User , on_delete = models.CASCADE)
chatroom = models.ForeignKey(ChatRoom , on_delete = models.CASCADE)
message = models.CharField(max_length = 200 )
created_timestamp = models.DateTimeField(auto_now = True)
updated_timestamp = models.DateTimeField(auto_now_add = True)
class Meta :
ordering = ["-created_timestamp"]
def __str__(self) :
return str(self.writer) " commented " str(self.message)[:10]
當我運行它時,我收到以下錯誤。
raise self.model.DoesNotExist(
django.contrib.auth.models.User.DoesNotExist: User matching query does not exist.
WebSocket DISCONNECT /ws/chat/12/ [127.0.0.1:54543]
在嘗試其他一些選項時:
僅使用此代碼位列印用戶并注釋 save_mesage 方法。
async def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json["message"]
username = text_data_json["username"]
roomId = text_data_json["roomId"]
roomName = text_data_json["roomName"]
user = User.objects.get(username = username)
print(user)
我收到此錯誤->
raise SynchronousOnlyOperation(message)
django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.
好吧,我不能使用同步方法來獲取異步程式中的資料,這完全沒問題。
但是當我嘗試這個時->
user = await User.objects.get(username = username)
print(user)
我在上面得到同樣的錯誤。
Again trying some other ways
like this
user = await database_sync_to_async(User.objects.get(username = username))()
print(user)
I get the same error .
again trying this ->
user = await sync_to_async(User.objects.get(username = username))()
print(user)
the same error arises .
Now I tried to access the user model data from the save_message function like this ->
@sync_to_async
def save_message(self , message , username , roomId , roomName) :
user = database_sync_to_async(User.objects.get(username = username))()
print(user)
I get this error ->
raise self.model.DoesNotExist(
django.contrib.auth.models.User.DoesNotExist: User matching query does not exist.
WebSocket DISCONNECT /ws/chat/12/ [127.0.0.1:58689]
Well talking about the user exists or not , I am the current logged in user in the app and I am only messeging . so there is no doubt the user does not exist .
Also trying this way ->
user = await User.objects.get(username = username)
await print(user)
this is the error ->
raise SynchronousOnlyOperation(message)
django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.
WebSocket DISCONNECT /ws/chat/12/ [127.0.0.1:65524]
This is the whole error log ->
D:\Programming\Python\Django project\chatsite\chat\consumers.py changed, reloading.
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
March 19, 2022 - 19:28:58
Django version 4.0.2, using settings 'chatsite.settings'
Starting ASGI/Channels version 3.0.4 development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
HTTP GET /chat/12/ 200 [0.06, 127.0.0.1:58686]
WebSocket HANDSHAKING /ws/chat/12/ [127.0.0.1:58689]
WebSocket CONNECT /ws/chat/12/ [127.0.0.1:58689]
Exception inside application: User matching query does not exist.
Traceback (most recent call last):
File "C:\Users\user\anaconda3\lib\site-packages\channels\staticfiles.py", line 44, in __call__
return await self.application(scope, receive, send)
File "C:\Users\user\anaconda3\lib\site-packages\channels\routing.py", line 71, in __call__
return await application(scope, receive, send)
File "C:\Users\user\anaconda3\lib\site-packages\channels\sessions.py", line 47, in __call__
return await self.inner(dict(scope, cookies=cookies), receive, send)
File "C:\Users\user\anaconda3\lib\site-packages\channels\sessions.py", line 263, in __call__
return await self.inner(wrapper.scope, receive, wrapper.send)
File "C:\Users\user\anaconda3\lib\site-packages\channels\auth.py", line 185, in __call__
return await super().__call__(scope, receive, send)
File "C:\Users\user\anaconda3\lib\site-packages\channels\middleware.py", line 26, in __call__
return await self.inner(scope, receive, send)
File "C:\Users\user\anaconda3\lib\site-packages\channels\routing.py", line 150, in __call__
return await application(
File "C:\Users\user\anaconda3\lib\site-packages\channels\consumer.py", line 94, in app
return await consumer(scope, receive, send)
File "C:\Users\user\anaconda3\lib\site-packages\channels\consumer.py", line 58, in __call__
await await_many_dispatch(
File "C:\Users\user\anaconda3\lib\site-packages\channels\utils.py", line 51, in await_many_dispatch
await dispatch(result)
File "C:\Users\user\anaconda3\lib\site-packages\channels\consumer.py", line 73, in dispatch
await handler(message)
File "C:\Users\user\anaconda3\lib\site-packages\channels\generic\websocket.py", line 194, in websocket_receive
await self.receive(text_data=message["text"])
File "D:\Programming\Python\Django project\chatsite\chat\consumers.py", line 33, in receive
await self.save_message(message , username , roomId , roomName)
File "C:\Users\user\anaconda3\lib\site-packages\asgiref\sync.py", line 414, in __call__
ret = await asyncio.wait_for(future, timeout=None)
File "C:\Users\user\anaconda3\lib\asyncio\tasks.py", line 442, in wait_for
return await fut
File "C:\Users\user\anaconda3\lib\concurrent\futures\thread.py", line 52, in run
result = self.fn(*self.args, **self.kwargs)
File "C:\Users\user\anaconda3\lib\site-packages\asgiref\sync.py", line 455, in thread_handler
return func(*args, **kwargs)
File "D:\Programming\Python\Django project\chatsite\chat\consumers.py", line 53, in save_message
user = database_sync_to_async(User.objects.get(username = username))()
File "C:\Users\user\anaconda3\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\user\anaconda3\lib\site-packages\django\db\models\query.py", line 439, in get
raise self.model.DoesNotExist(
django.contrib.auth.models.User.DoesNotExist: User matching query does not exist.
WebSocket DISCONNECT /ws/chat/12/ [127.0.0.1:58689]
I have tried every possible combination whether valid or not but unable to get the running program . Kindly help me . I am stuck it for days. It's going to be a week after a day.
Any help would be great. Thanks
uj5u.com熱心網友回復:
其實程式沒有問題。問題在于通過 websockets 的 json 資料的回傳資料型別。訊息通過websockets發送時,以json資料的形式發送,鍵值對中有字串型別。因此,當發送資料時,額外的引號也被考慮在內,并且資料不僅僅是字串型別的用戶名,實際上是在末尾帶有額外引號的字串的形式。例如 -> 當前用戶的用戶名是 xyz123 ,則以 "xyz123" 的形式發送。解決方案就是截斷最后兩個引號,我們就完成了。
這是作業代碼。
@sync_to_async
def save_message(self , message , username, roomId , roomName ) :
username = username[1:-1]
roomName = roomName[1:-1]
user = User.objects.get(username = str(username))
room = ChatRoom.objects.get(roomname = roomName, id = int(roomId))
message = ChatMessage(user = user , chatroom = room , message = str(message))
message.save()
print(message)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/448911.html
