Django Channels - Websocket 连接失败

Jar*_*r95 3 python django websocket django-channels

我目前正在尝试设置我的 Django Channels 聊天应用程序,并且我已经尽我所能地遵循了文档 - 显然,当出现问题时,总有解决方案,但我只是很难找出我的位置让这个工作出错了。

\n

到目前为止,我已经安装了 Django Channels,在 settings.py 中正确配置了我的channel_layer,我已经参考了文档和其他在线教程来创建我的consumer.py 文件、utils.py、views.py 和chat.html 模板文件。

\n

我还配置了 nginx、gunicorn、daphne 和 redis,我可以确认所有这些服务都在运行。我已经安装了 LetsEncrypt 并允许使用 SSL 端口(https 连接为 443)。

\n

我目前正在实时生产服务器上访问我的 Django 网站,如下所示:https://mydomain:8001,并且我的聊天的直接链接配置为 https://mydomain:8001/messenger

\n

(我已允许端口 8001 通过我的防火墙)

\n

以下是我从聊天页面收到的状态消息:

\n
SUCCESS {response: "Successfully got the chat.", chatroom_id: 1}chatroom_id: 1response: "Successfully got the chat."__proto__: Object\n(index):303 setupWebSocket: 1\n(index):371 ChatSocket connecting..\nWebSocket connection to 'wss://www.<mydomain>.com:8001/messenger/1/' failed: \nsetupWebSocket @ (index):317\nsuccess @ (index):389\nc @ jquery.min.js\nfireWith @ jquery.min.js\nl @ jquery.min.js\n(anonymous) @ jquery.min.js\nChatSocket error Event\xc2\xa0{isTrusted: true, type: "error", target: WebSocket, currentTarget: WebSocket, eventPhase: 2,\xc2\xa0\xe2\x80\xa6}bubbles: falsecancelBubble: falsecancelable: falsecomposed: falsecurrentTarget: WebSocket\xc2\xa0{url: "wss://<mydomain>:8001/messenger/1/", readyState: 3, bufferedAmount: 0, onopen: null, onOpen: \xc6\x92,\xc2\xa0\xe2\x80\xa6}defaultPrevented: falseeventPhase: 0isTrusted: truepath: []returnValue: truesrcElement: WebSocket\xc2\xa0{url: "wss://<mydomain>:8001/messenger/1/", readyState: 3, bufferedAmount: 0, onopen: null, onOpen: \xc6\x92,\xc2\xa0\xe2\x80\xa6}target: WebSocket\xc2\xa0{url: "wss://<mydomain>:8001/messenger/1/", readyState: 3, bufferedAmount: 0, onopen: null, onOpen: \xc6\x92,\xc2\xa0\xe2\x80\xa6}timeStamp: 43973.62500001327type: "error"__proto__: Event\n(index):357 Chat socket closed.\n
Run Code Online (Sandbox Code Playgroud)\n

任何有关解决此问题的帮助将不胜感激。如果您可以建议您应该查看哪些文件,我将很乐意使用我的聊天应用程序所需的任何文件的内容来更新我的问题。:-)

\n

谢谢你!

\n

文件内容:

\n

nginx.conf

\n
server {\n    listen 80;\n    server_name myapp mydomain.com www.mydomain.com;\n\n    location = /favicon.ico { access_log off; log_not_found off; }\n    location /static/ {\n        root /home/myapp;\n    }\n\n    location / {\n        proxy_set_header Host $http_host;\n        proxy_set_header X-Real-IP $remote_addr;\n        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n        proxy_set_header X-Forwarded-Proto $scheme;\n        proxy_pass http://unix:/home/myapp/myapp.sock;\n    }\n    \n    location /ws/ {\n        proxy_http_version 1.1;\n        proxy_set_header Upgrade $http_upgrade;\n        proxy_set_header Connection "upgrade";\n        proxy_redirect off;\n        proxy_pass https://127.0.0.1:8001;\n    }\n    \n    location /wss/ {\n                proxy_set_header X-Real-IP $remote_addr;\n                proxy_set_header X-Forwarded-for $proxy_add_x_forwarded_for;\n                proxy_set_header Host $http_host;\n                proxy_pass http://127.0.0.1:8001;\n                proxy_http_version 1.1;\n                proxy_set_header Upgrade $http_upgrade;\n                proxy_set_header Connection "upgrade";\n        }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

路由.py

\n
from channels.auth import AuthMiddlewareStack\nfrom channels.routing import ProtocolTypeRouter, URLRouter\nfrom channels.security.websocket import AllowedHostsOriginValidator\nfrom django.urls import path, re_path\n\nfrom messenger.consumers import ChatConsumer\n\n\napplication = ProtocolTypeRouter({\n    'websocket': AllowedHostsOriginValidator(\n        AuthMiddlewareStack(\n            URLRouter([\n                \n                    path('messenger/<room_id>/', ChatConsumer),\n                    \n            ])\n        )\n    ),\n})\n
Run Code Online (Sandbox Code Playgroud)\n

设置.py

\n
CHANNEL_LAYERS = {\n    'default': {\n        'BACKEND': 'channels_redis.core.RedisChannelLayer',\n        'CONFIG': {\n            "hosts": [('127.0.0.1', 6379)],\n        },\n    },\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我在控制台中收到错误:(index):317 WebSocket connection to 'wss://www.mydomain.com:8001/messenger/1/' failed:

\n

(其中显示 mydomain.com,我的实际域名就在那里输入:)

\n

最新的达芙妮原木来自journalctl

\n
Jun 21 14:22:56 myhostname.com python3[36088]: 2021-06-21 14:22:56,576 INFO     Starting server at ssl:8001:privateKey=/etc/letsencrypt/live/mydomain.com/privkey.pem:certKey=/etc/let>\nJun 21 14:22:56 myhostname.com python3[36088]: 2021-06-21 14:22:56,576 INFO     HTTP/2 support not enabled (install the http2 and tls Twisted extras)\nJun 21 14:22:56 myhostname.com python3[36088]: 2021-06-21 14:22:56,577 INFO     Configuring endpoint ssl:8001:privateKey=/etc/letsencrypt/live/mydomain.com/privkey.pem:certKey=/etc/l>\nJun 21 14:22:56 myhostname.com python3[36088]: 2021-06-21 14:22:56,584 INFO     Listening on TCP address 0.0.0.0:8001\n
Run Code Online (Sandbox Code Playgroud)\n

达芙妮日志中的错误

\n
Jun 21 14:38:51 myhostname.com python3[36088]: Traceback (most myhostname.com python3[36088]:   File "/usr/local/lib/python3.6/site-packages/channels/routing.py", line 71, in __call__\nJun 21 14:38:51 myhostname.com python3[36088]:     return await application(scope, receive, send)\nJun 21 14:38:51 myhostname.com python3[36088]:   File "/usr/local/lib/python3.6/site-packages/channels/security/websocket.py", line 37, in __call__\nJun 21 14:38:51 myhostname.com python3[36088]:     return await self.application(scope, send, receive)\nJun 21 14:38:51 myhostname.com python3[36088]:   File "/usr/local/lib/python3.6/site-packages/channels/sessions.py", line 47, in __call__\nJun 21 14:38:51 myhostname.com python3[36088]:     return await self.inner(dict(scope, cookies=cookies), receive, send)\nJun 21 14:38:51 myhostname.com python3[36088]:   File "/usr/local/lib/python3.6/site-packages/channels/sessions.py", line 254, in __call__\nJun 21 14:38:51 myhostname.com python3[36088]:     return await self.inner(wrapper.scope, receive, wrapper.send)\nJun 21 14:38:51 myhostname.com python3[36088]:   File "/usr/local/lib/python3.6/site-packages/channels/auth.py", line 181, in __call__\nJun 21 14:38:51 myhostname.com python3[36088]:     return await super().__call__(scope, receive, send)\nJun 21 14:38:51 myhostname.com python3[36088]:   File "/usr/local/lib/python3.6/site-packages/channels/middleware.py", line 26, in __call__\nJun 21 14:38:51 myhostname.com python3[36088]:     return await self.inner(scope, receive, send)\nJun 21 14:38:51 myhostname.com python3[36088]:   File "/usr/local/lib/python3.6/site-packages/channels/routing.py", line 160, in __call__\nJun 21 14:38:51 myhostname.com python3[36088]:     send,\nJun 21 14:38:51 myhostname.com python3[36088]:   File "/usr/local/lib/python3.6/site-packages/asgiref/compatibility.py", line 33, in new_application\nJun 21 14:38:51 myhostname.com python3[36088]:     instance = application(scope)\nJun 21 14:38:51 myhostname.com python3[36088]:   File "/usr/local/lib/python3.6/site-packages/channels/generic/websocket.py", line 159, in __init__\nJun 21 14:38:51 myhostname.com python3[36088]:     super().__init__(*args, **kwargs)\nJun 21 14:38:51 myhostname.com python3[36088]: TypeError: object.__init__() takes no parameters\n\n
Run Code Online (Sandbox Code Playgroud)\n

Ale*_*nov 8

我认为问题出在配置上。在nginx中,你必须指定URL前缀,而不是协议,也不需要同时添加wswss。nginx 配置必须与此类似

location /ws/ {
    proxy_set_header Host               $http_host;
    proxy_set_header X-Real-IP          $remote_addr;
    proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host   $server_name;
    proxy_set_header X-Forwarded-Proto  $scheme;
    proxy_set_header X-Url-Scheme       $scheme;
    proxy_redirect off;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;

    proxy_pass http://127.0.0.1:8001;
}
Run Code Online (Sandbox Code Playgroud)

因此,要通过 Websocket 协议进行连接,您必须访问wss://www.<mydomain>.com:8001/ws/. 为此,您的 asgi 路由还必须包含ws前缀。另外,您应该as_asgi()对消费者使用。(https://channels.readthedocs.io/en/stable/topics/routing.html

application = ProtocolTypeRouter({
    'websocket': AllowedHostsOriginValidator(
        AuthMiddlewareStack(
            URLRouter([
                    path('ws/messenger/<room_id>/', ChatConsumer.as_asgi()), 
            ])
        )
    ),
})
Run Code Online (Sandbox Code Playgroud)

现在您应该能够连接到wss://www.<mydomain>.com:8001/ws/messenger/1/(不要忘记ws!)。