Django Channels __init__() 得到了意外的关键字参数“scope”

问题描述 投票:0回答:2

我一直在尝试根据教程建立一个基本的 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)],
        },
    },
}
python django websocket django-channels
2个回答
1
投票

尝试仔细检查你的逻辑,因为文件看起来不错:当我看到这个时,我错误地将我的

uwsgi.application
传递给了daphne,而不是
asgi.application


0
投票

对于将来查找此问题的任何人来说,此问题是因为未运行开发服务器。 确保您的 django 开发服务器(python manage.py runserver)和 redis 都已启动并正在运行。

© www.soinside.com 2019 - 2024. All rights reserved.