在 wss 000-default.conf:asgi.py:consumers.py:settings.py:js 文件:js 响应:daphne 命令:午餐后达芙妮的回复:

问题描述

操作系统 Ubuntu 20 LTS

我的项目在生产中遇到问题,我不明白哪里出了问题,出了什么问题,这些是文件和响应:

000-default.conf:

<VirtualHost *:80>

.....

    RewriteEngine on

    RewriteCond %{SERVER_NAME} =site.addns.org [OR]
    RewriteCond %{SERVER_NAME} =www.site.addns.org
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

    RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC,OR]
    RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
    RewriteRule .* wss://%{SERVER_NAME}%{REQUEST_URI} [P,QSA,L]
    ProxyPass /wss/ wss://127.0.0.1:8000/
    ProxyPassReverse /wss/ wss://127.0.0.1:8000/



</VirtualHost>

<VirtualHost *:443>
.....


     ProxyPreserveHost On
     SSLProxyEngine on
     SSLProxyVerify none
     SSLProxyCheckPeerCN off
     SSLProxyCheckPeerName off

    RewriteEngine on
    RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC,OR]
    RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
    RewriteRule .* ws://127.0.0.1:8000%{REQUEST_URI} [P,L]
    ProxyPass /wss/ wss://127.0.0.1:8000/
    ProxyPassReverse /wss/ wss://127.0.0.1:8000/
    


</VirtualHost>

asgi.py:

import os

from django.urls import re_path,path
from django.core.asgi import get_asgi_application


os.environ.setdefault("DJANGO_SETTINGS_MODULE","produzione.settings")
django_asgi_app = get_asgi_application()

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter,URLRouter
from channels.routing import get_default_application
from principale import consumers

application = ProtocolTypeRouter({
    "http": django_asgi_app,"websocket": AuthMiddlewareStack(
        URLRouter([
            path(r'wss/$',consumers.EchoConsumer.as_asgi()),path(r'ws/chat/$',])
    ),})

consumers.py:

import json
from channels.generic.websocket import WebsocketConsumer

from channels.consumer import AsyncConsumer

class EchoConsumer(WebsocketConsumer):

    def connect(self):
        self.accept()

settings.py:

...
Asgi_APPLICATION = "produzione.asgi.application"
CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer','CONfig': {
            "hosts": [('127.0.0.1',8000)],},}
...

js 文件

    var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
    var chat_socket = new WebSocket(ws_scheme + '://' + window.location.host + '/wss/');

    var chatSocket = new WebSocket(
            'wss://'
            + '127.0.0.1:8000'
            + '/wss'
        );

js 响应:

错误立即出现在浏览器控制台上

WebSocket connection to 'wss://site.addns.org/wss/' Failed: Error during WebSocket handshake: Unexpected response code: 404 

错误立即出现在 error.log 文件中:

Not Found: /wss/

在浏览器控制台几秒钟后出现这个

WebSocket connection to 'wss://127.0.0.1:8000/wss' Failed: WebSocket opening handshake timed out

daphne 命令:

sudo daphne -e ssl:8001:privateKey=privkey.pem:certKey=cert.pem produzione.asgi:application -b 127.0.0.1

午餐后达芙妮的回复


2021-02-11 14:03:42,587 INFO     Starting server at ssl:8001:privateKey=privkey.pem:certKey=cert.pem,tcp:port=8000:interface=127.0.0.1
2021-02-11 14:03:42,587 INFO     HTTP/2 support enabled
2021-02-11 14:03:42,588 INFO     Configuring endpoint ssl:8001:privateKey=privkey.pem:certKey=cert.pem
2021-02-11 14:03:42,592 INFO     Listening on TCP address 0.0.0.0:8001
2021-02-11 14:03:42,592 INFO     Configuring endpoint tcp:port=8000:interface=127.0.0.1
2021-02-11 14:03:42,593 INFO     Listening on TCP address 127.0.0.1:8000

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)