我一直在尝试根据本教程设置一个基本的Django Channels项目,但出现此错误。我使用Django python shell测试Redis是否正常工作。我正在使用Daphne my_project.asgi:application运行Daphne。我找不到任何类似的问题或有关此问题的文档。如果您需要查看代码的其他部分,请告诉我。任何帮助将不胜感激!
consumers.py
from channels.generic.websocket import WebsocketConsumer
from asgiref.sync import async_to_sync
import json
class TestConsumer(WebsocketConsumer):
def connect(self):
self.group_name = 'test'
async_to_sync(self.channel_layer.group_add)(
self.group_name,
self.channel_name
)
self.accept()
def disconnect(self, close_code):
async_to_sync(self.channel_layer.group_discard)(
self.group_name,
self.channel_name
)
def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json['message']
async_to_sync(self.channel_layer.group_send)(
self.group_name,
{
'type': 'chat_message',
'message': message
}
)
def chat_message(self, event):
message = event['message']
self.send(text_data=json.dumps({
'message' : message
}))
project routing.py
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
import main.routing
import sockets.routing
application = ProtocolTypeRouter
({
'websocket' : AuthMiddlewareStack(
URLRouter(
sockets.routing.websocket_urlpatterns
)
)
})
asgi.py
import os
import django
from channels.routing import get_default_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'socialapp.settings')
django.setup()
application = get_default_application()
project settings
ASGI_APPLICATION = 'socialapp.routing.application'
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("localhost", 6379)],
},
},
}
答案 0 :(得分:1)
请尝试仔细检查逻辑,因为文件看起来不错:当我看到此文件时,我错误地将256dp
传递给了达芙妮而不是uwsgi.application