Web套接字无法在Heroku上的Django应用中工作

时间:2019-01-14 21:55:15

标签: django heroku websocket redis channels

我正在尝试在heroku上运行我的第一个django应用程序,我成功地成功部署了该应用程序,除了Web套接字之外,其他所有功能都可以正常工作。 我在本地使用内存通道,一切正常。我在内存通道和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凭据:

  • 主机:ec2-34-254-133-4.eu-west-1.compute.amazonaws.com
  • 用户
  • 端口26849
  • 密码
  • URI:redis:// h:p69877187246d4f0b36a94488814ee0b372234a2e943a61df0cdc84e4f85f35f2@ec2-34-254-133-4.eu-west-1.compute.amazonaws.com:26849

procfile:

web: gunicorn battleships.wsgi --preload

heroku日志:

  

2019-01-14T21:06:52.847189 + 00:00 heroku [web.1]:状态从上更改   至开始时间2019-01-14T21:07:00.911326 + 00:00 heroku [web.1]:开始   使用命令gunicorn battleships.wsgi --preload处理   2019-01-14T21:07:04.629926 + 00:00 heroku [web.1]:状态从   开始了2019-01-14T21:07:06.453341 + 00:00 heroku [web.1]:处理   退出,状态为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服务= 374ms status = 200字节= 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服务= 163ms status = 200字节= 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服务= 31ms status = 200字节= 290协议= 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 = d4c9a​​31a-0dc6-436a-b35b-054e94770a5e fwd =“ 80.49.199.124”   dyno = web.1 connect = 0ms服务= 30ms status = 200字节= 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服务= 267ms status = 500字节= 149564   协议= http 2019-01-14T21:07:03 + 00:00 app [heroku-redis]:   来源= REDIS插件= 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服务= 25ms status = 200字节= 1310   protocol = http

有人可以帮助我吗,或者建议其他方法?

1 个答案:

答案 0 :(得分:0)

我不认为z-index支持gunicorn(我不确定)。但是,我做到了,它奏效了。请记住,我没有使用通道层:

asgi.py

asgi

procfile

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()