我正在尝试在 heroku 上运行我的第一个 django 应用程序,我成功地部署了该应用程序,并且除了 Web 套接字之外一切正常。
在本地,我在内存 channel 中使用过,一切正常。我在内存 channel 和redis中都尝试了heroku,但两者都不起作用。我尝试了许多不同的配置,但我找不到任何可行的解决方案。此外,heroku 日志让我不知道可能出了什么问题..
我在浏览器中遇到的错误:
join_game.js:222 WebSocket connection to 'wss://battleships-war.herokuapp.com/game/20' failed: Error during WebSocket handshake: Unexpected response code: 404
Javascript客户端代码:
[我尝试了 ws 和 wss 前缀,但都崩溃了]
var ws = new WebSocket((window.location.protocol == 'http') ? 'ws://' : 'wss://' + window.location.host + '/game/' + gameId);
在 settings.py 中配置 redis
CHANNEL_LAYERS = {
"default": {
"BACKEND": "asgi_redis.RedisChannelLayer",
"CONFIG": {
"hosts": [('ec2-34-254-133-4.eu-west-1.compute.amazonaws.com', 26849)],
},
'ROUTING': 'battleships.routing.channel_routing',
}, }
在本地工作的内存配置中可选:
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'asgiref.inmemory.ChannelLayer',
'ROUTING': 'battleships.routing.channel_routing',
},
}
来自heroku的redis凭据:
过程文件:
web: gunicorn battleships.wsgi --preload
heroku 日志:
2019-01-14T21:06:52.847189+00:00 heroku[web.1]: State changed from up to starting 2019-01-14T21:07:00.911326+00:00 heroku[web.1]: Starting process with command
gunicorn battleships.wsgi --preload
2019-01-14T21:07:04.629926+00:00 heroku[web.1]: State changed from starting to up 2019-01-14T21:07:06.453341+00:00 heroku[web.1]: Process exited with status 0 2019-01-14T21:07:51.531270+00:00 heroku[router]: at=info method=GET path="/new_game/" host=battleships-war.herokuapp.com request_id=45707e08-cfd9-458c-8ce4-0f423acfb632 fwd="80.49.199.124" dyno=web.1 connect=0ms service=374ms status=200 bytes=1307 protocol=http 2019-01-14T21:07:53.241606+00:00 heroku[router]: at=info method=GET path="/new_game/" host=battleships-war.herokuapp.com request_id=a77411fc-2ab6-4be7-b4c9-e86d9440c3e1 fwd="80.49.199.124" dyno=web.1 connect=0ms service=163ms status=200 bytes=1307 protocol=http 2019-01-14T21:07:57.454852+00:00 heroku[router]: at=info method=POST path="/new_game/" host=battleships-war.herokuapp.com request_id=76aab54e-4508-4485-90de-e66e7bb4715b fwd="80.49.199.124" dyno=web.1 connect=0ms service=31ms status=200 bytes=290 protocol=http 2019-01-14T21:08:01.604730+00:00 heroku[router]: at=info method=GET path="/game/22/dfgdfgdfgdf" host=battleships-war.herokuapp.com request_id=d4c9a31a-0dc6-436a-b35b-054e94770a5e fwd="80.49.199.124" dyno=web.1 connect=0ms service=30ms status=200 bytes=1310 protocol=http 2019-01-14T21:08:02.002519+00:00 heroku[router]: at=info method=GET path="/game/22/dfgdfgdfgdf/join" host=battleships-war.herokuapp.com request_id=3c682a1a-f203-493a-853d-4a4ff9cccaf8 fwd="80.49.199.124" dyno=web.1 connect=0ms service=267ms status=500 bytes=149564 protocol=http 2019-01-14T21:07:03+00:00 app[heroku-redis]: source=REDIS addon=redis-octagonal-92972 sample#active-connections=1 sample#load-avg-1m=0.09 sample#load-avg-5m=0.115 sample#load-avg-15m=0.125 sample#read-iops=0 sample#write-iops=0 sample#memory-total=15664336kB sample#memory-free=11855124kB sample#memory-cached=1580208kB sample#memory-redis=278200bytes sample#hit-rate=0 sample#evicted-keys=0 2019-01-14T21:08:07.295235+00:00 heroku[router]: at=info method=GET path="/game/22/dfgdfgdfgdf" host=battleships-war.herokuapp.com request_id=789356b5-acb3-48e7-9dd1-be5250a7445d fwd="80.49.199.124" dyno=web.1 connect=0ms service=25ms status=200 bytes=1310 protocol=http
任何人都可以帮助我或提出不同的方法吗?
最佳答案
我不认为gunicorn
支持asgi
(我不知道)。但是,我这样做了,它奏效了。请记住,我没有使用 channel 层:
asgi.py
import os
import django
from channels.routing import get_default_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
django.setup()
application = get_default_application()
过程文件web: daphne project.asgi:application --port $PORT --bind 0.0.0.0
关于django - Web Sockets 在 Heroku 上的 Django 应用程序中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54189777/