我在 DigitalOcean 的应用程序平台上运行 websockets 时遇到问题。我认为我从根本上缺少与达芙妮相关的配置。
这是我的设置:
达芙妮4.0.0 通道-Redis 3.4.1
设置.py
INSTALLED_APPS = [
...
# Third-party
'channels',
...
WSGI_APPLICATION = "config.wsgi.application"
ASGI_APPLICATION = "config.asgi.application"
CSRF_TRUSTED_ORIGINS = [
'https://blah.com',
'https://*.blah.com',
]
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
ASGI.py
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.dev')
import django
django.setup()
from django.core.asgi import get_asgi_application
asgi_app = get_asgi_application()
import api_waitlist.routing
from channels.routing import ProtocolTypeRouter, URLRouter, get_default_application
from channels.security.websocket import AllowedHostsOriginValidator
from channels.auth import AuthMiddlewareStack
application = ProtocolTypeRouter({
'http': asgi_app,
'websocket':
AllowedHostsOriginValidator(AuthMiddlewareStack(
URLRouter(
api_waitlist.routing.websocket_urlpatterns
))
), # new
})
DigitalOcean 配置
数字海洋应用程序: “运行命令”位于应用程序>设置>运行命令下:
daphne -b 0.0.0.0 -p 8080 config.asgi:application
Redis配置: Redis 是使用 DigitalOcean 的托管 Redis 数据库进行设置的。连接字符串由 DigitalOcean 提供。包含连接字符串的应用程序级别变量“REDIS_URL”是:
rediss://default:password_here@fs-dev-redis-do-user-xxxx-0.b.db.ondigitalocean.com:25061
React 前端(React 位于 Django 项目脚手架内): 建立websocket连接的React前端代码是:
wb = new WebSocket("ws://127.0.0.1:8000/ws/waitlist/");
我能够专门针对 DigitalOcean 应用程序平台解决此问题:
最终 URL 如下所示:
wss://your_domain.com/ws/waitlist/
抱歉我之前的评论,回答一个问题。看来你自己找到了解决方案。
但我仍然认为有些东西丢失了或者没有被你指定:
我在 INSTALLED_APPS 中没有看到“daphne” - 仅在频道中。
DO 支持使用 socket.io 的 WebSocket,但不支持长轮询 [https://docs.digitalocean.com/developer-center/deploy-an-app-using- websockets 到应用程序平台/]
您也可以分享您的consumer.py 文件吗?