6

你好真棒人!

我创建了一个聊天室django-channels。每次我尝试在生产中通过网络套接字连接到我的聊天室时,它都会失败。在本地它可以正常工作。

我在digitalocean上主持

点冻结:

channels==2.1.2
channels-redis==2.3.0
daphne==2.2.1
'''

我已经安装redis-server

sudo apt-get install redis-server

这是我的设置。

INSTALLED_APPS = [
    # '''
   'channels',
    # '''
] 
CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            "hosts": [os.environ.get('REDIS_URL', 'redis://localhost:6379')],
        },
    },
}
ASGI_APPLICATION = "project_name.routing.application"

这是我的asgi.py旁边wsgi.py

import os
import django
from channels.routing import get_default_application

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings")
django.setup()
application = get_default_application()

这是我的project_folder.rounting.py

application = ProtocolTypeRouter({
    'websocket':AllowedHostsOriginValidator(
        AuthMiddlewareStack(
            URLRouter([
                # my urls
            ])
        )
    )
})

我一直在 Firefox 中得到这个,在其他浏览器中也有类似的东西:

Firefox 无法与位于 wss://www.domain_name.com/url-to/1/XBv​​Zjr2pqdf6fhy/ 的服务器建立连接

但是它在本地工作。

更新

这是我的js

var loc = window.location;
var wsStart = loc.protocol == "https:" ? "wss://" : "ws://"
var endpoint = wsStart + loc.host + loc.pathname
var socket = new ReconnectingWebSocket(endpoint);

socket.onmessage = function(e){
    // code
}
4

2 回答 2

15

我终于解决了这个问题,并wssslwss://.

对于那些面临同样问题的人。

请注意,我使用

  • gunicorn作为网络服务器仅用于http请求
  • daphne作为ws网络套接字的网络服务器
  • nginx作为反向代理

asgi.py

import os
import django
from channels.routing import get_default_application

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
django.setup()
application = get_default_application()

settings.py

ASGI_APPLICATION = "project.routing.application"

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            "hosts": [(os.environ.get('REDIS_HOST', 'localhost'),6379)],
        },
    },
}

asgi在用 django设置好之后,我用它supervisorctl来继续daphne运行。daphne_asgi.conf在中创建文件/etc/supervior/conf.d/

daphne_asgi.conf

[program:asgi_daphne]

directory=/path/to/your/project

command=/executable/path/to/daphne --bind 0.0.0.0 --port 8010 project.asgi:application
# 0.0.0.0 ip of your website
# I choose the port 8010 for daphne

stdout_logfile=/path/to/log/daphne.log

autostart=true

autorestart=true

redirect_stderr=true

运行以下命令来更新启动守护进程

sudo supervisorctl reread
sudo supervisorctl update

这里是配置nginx

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}
upstream websocket {
    server 0.0.0.0:8010;
}

daphne 中使用的主机和端口...--bind 0.0.0.0 --port 8010

#redirection to a https
server {
    listen 80;
    server_name 0.0.0.0 example.com www.example.com;
    client_max_body_size 10M;
    return 301 https://www.example.com$request_uri;
}

server {
    listen 443 ssl default_server;
    server_name www.example.com;
    client_max_body_size 10M;

    # ssl configuration
    ...

    # normal http request, I use .sock
    location / {
        include proxy_params;
        proxy_pass http://unix:/path/to/project.sock;
    }

    # ws request /ws/
    location /ws/ {
        proxy_pass http://websocket;

         # this magic is needed for WebSocket
        proxy_http_version  1.1;
        proxy_set_header    Upgrade $http_upgrade;
        proxy_set_header    Connection $connection_upgrade;
        proxy_set_header    Host $http_host;
        proxy_set_header    X-Real-IP $remote_addr;
    }
}

请注意,我/ws/在我的 ws 网址中添加了

application = ProtocolTypeRouter({

    'websocket':AllowedHostsOriginValidator(
        AuthMiddlewareStack(
            URLRouter(
                [
                    url(r'^ws/$', HelloConsumer),
                ]
            )
        )
    )
})
于 2018-10-15T19:33:39.410 回答
2

最重要的是,使用 redis 5.0.9。否则会再次出现错误。

github.com/tporadowski/redis/releases获取

于 2020-08-09T06:32:07.113 回答