19

这真是令人沮丧,我确实根据文档设置了所有内容,但是当我尝试独立运行它时,daphne 不断抛出错误,当我使用python manage.py run server. 这非常令人沮丧,我似乎在其他任何地方都找不到类似的错误

2020-01-25 09:57:17,627 INFO     Starting server at tcp:port=8000:interface=127.0.0.1
2020-01-25 09:57:17,628 INFO     HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2020-01-25 09:57:17,628 INFO     Configuring endpoint tcp:port=8000:interface=127.0.0.1
2020-01-25 09:57:17,629 INFO     Listening on TCP address 127.0.0.1:8000
127.0.0.1:44516 - - [25/Jan/2020:09:57:27] "WSCONNECTING /ws/score/" - -
2020-01-25 09:57:28,637 ERROR    Exception inside application: Django can only handle ASGI/HTTP connections, not websocket.
  File "/home/sofdeath/.local/lib/python3.7/site-packages/daphne/cli.py", line 30, in asgi
    await self.app(scope, receive, send)
  File "/home/sofdeath/.local/lib/python3.7/site-packages/django/core/handlers/asgi.py", line 146, in __call__
    % scope['type']
  Django can only handle ASGI/HTTP connections, not websocket.
127.0.0.1:44516 - - [25/Jan/2020:09:57:28] "WSDISCONNECT /ws/score/" - -
^C2020-01-25 09:57:39,126 INFO     Killed 0 pending application instances

这是我的 asgi.py

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tabulator.settings')

application = get_asgi_application()

我的路由.py:

from django.urls import re_path
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.security.websocket import AllowedHostsOriginValidator, OriginValidator
import judge.routing

application = ProtocolTypeRouter({
    # (http->django views is added by default)
    'websocket': AllowedHostsOriginValidator(
        AuthMiddlewareStack(
            URLRouter(
                judge.routing.websocket_urlpatterns
            )
        )
    ),
})

在我的 settings.py 中:

INSTALLED_APPS = [
    ...
    'channels',
]
...
WSGI_APPLICATION = 'tabulator.wsgi.application'
ASGI_APPLICATION = "tabulator.routing.application"
...
CHANNEL_LAYERS = {
    "default": {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            "hosts": [('127.0.0.1', 6379)],
        },
    },
}

4

3 回答 3

31

把你改成asgi.py这样:

import os
import django
from channels.routing import get_default_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tabulator.settings')

django.setup()

application = get_default_application()
于 2020-01-25T12:15:48.580 回答
0

您需要根据此更改您的 asgi.py

# mysite/asgi.py
import os

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
import chat.routing

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

application = ProtocolTypeRouter({
  "http": get_asgi_application(),
  "websocket": AuthMiddlewareStack(
        URLRouter(
            chat.routing.websocket_urlpatterns
        )
    ),
})
于 2022-02-28T15:02:17.867 回答
-1

asgi.py像@ahmad 所说的那样改变你的,然后编辑你settings.py的:

CHANNEL_LAYERS = {
    "default": {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            "hosts": [("redis", 6379)],
        },
    },
}

并确保DEBUG = False。那是因为你没有在本地服务器上运行你的项目,如果你使用它会抛出一个错误"127.0.0.1"or "localhost"

于 2020-02-02T07:49:41.190 回答