我想创建聊天应用程序,请在此处遵循https://channels.readthedocs.io/en/latest/tutorial/part_2.html,
chat/
__init__.py
routing.py
urls.py
settings.py
wsgi.py
我将此代码添加到我的routing.py
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
import chat.routing
application = ProtocolTypeRouter({
# (http->django views is added by default)
'websocket': AuthMiddlewareStack(
URLRouter(
chat.routing.websocket_urlpatterns
)
),
})
在我的settings.py中
ASGI_APPLICATION = 'Accounting.routing.application'
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('127.0.0.1', 6379)],
},
},
}
在我的urls.py中
urlpatterns = [
path('chat/', include('chat.urls')),
path('admin/', admin.site.urls),
]
在我的聊天应用中
chat/
__init__.py
consumers.py
routing.py
templates/
chat/
index.html
room.html
urls.py
views.py
我有consumers.py
from asgiref.sync import async_to_sync
from channels.generic.websocket import WebsocketConsumer
import json
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']
# Send message to room group
async_to_sync(self.channel_layer.group_send)(
self.room_group_name,
{
'type': 'chat_message',
'message': message
}
)
# Receive message from room group
def chat_message(self, event):
message = event['message']
# Send message to WebSocket
self.send(text_data=json.dumps({
'message': message
}))
这是我的chat> routing.py中的代码
from django.urls import re_path
from . import consumers
websocket_urlpatterns = [
re_path(r'ws/chat/(?P<room_name>\w+)/$', consumers.ChatConsumer),
]
在我的chat> views.py中
from django.shortcuts import render
def index(request):
return render(request, 'chat/index.html', {})
def room(request, room_name):
return render(request, 'chat/room.html', {
'room_name': room_name
})
我的聊天应用程序中有urls.py
从django.urls导入路径
from . import views
app_name = 'chat'
urlpatterns = [
path('', views.index, name='index'),
path('<str:room_name>/', views.room, name='room'),
]
我遵循所有方向,我复制粘贴代码,py的位置以及所有内容,但是仍然收到此错误
我错过了什么?
更新
当我在pycharm终端docker中尝试运行-p 6379:6379 -d redis:2.8
这里有些问题。
1)你是混合async
和sync
消费者的代码。
我建议只使用async consumer
您所有的方法,然后应该是async
方法。
看起来您是get_thread
从同步上下文中调用的,但get_thread
已被包裹在其中,database_sync_to_async
因此需要从异步上下文中调用(并且需要等待)。
您还需要 await
self.channel_layer.group_add
2)在您ThreadView
拥有一个ChatMessage.objects.create
这里的同时,您还应该通过线程通道组发送一条消息。
from channels.layers import get_channel_layer
channel_layer = get_channel_layer()
async_to_sync(channel_layer.group_send)(f"thread_{thread.id}", {"type": "chat.message", "text": ...})
您还需要将chat消息保存在websocket_receive中的数据库中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句