Flask-Socketio:WebSocket 握手期间出错:意外的响应代码:503 & WebSocket 在建立连接之前关闭

Flask-Socketio:Error during WebSocket handshake: Unexpected response code: 503 & WebSocket is closed before the connection is established

所以,我正在尝试将我的聊天应用程序部署到 Heroku 我看到了一些答案,但是 none 其中有 flask-socketio 并且它们有你不能在 flask-socketio 中做的解决方案。我按照 here 中的步骤操作。此外,您可以在日志中发现错误

2020-07-09T14:55:03.811700+00:00 app[web.1]: Traceback (most recent call last):
2020-07-09T14:55:03.811746+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/eventlet/hubs/hub.py", line 461, in fire_timers
2020-07-09T14:55:03.811747+00:00 app[web.1]: timer()
2020-07-09T14:55:03.811751+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/eventlet/hubs/timer.py", line 59, in __call__
2020-07-09T14:55:03.811752+00:00 app[web.1]: cb(*args, **kw)
2020-07-09T14:55:03.811754+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/eventlet/semaphore.py", line 147, in _do_acquire
2020-07-09T14:55:03.811754+00:00 app[web.1]: waiter.switch()
2020-07-09T14:55:03.811783+00:00 app[web.1]: File "src/gevent/greenlet.py", line 858, in gevent._gevent_cgreenlet.Greenlet.run
2020-07-09T14:55:03.811811+00:00 app[web.1]: File "src/gevent/greenlet.py", line 829, in gevent._gevent_cgreenlet.Greenlet._Greenlet__report_result
2020-07-09T14:55:03.811814+00:00 app[web.1]: File "src/gevent/_gevent_cgreenlet.pxd", line 45, in gevent._gevent_cgreenlet.get_my_hub
2020-07-09T14:55:03.811863+00:00 app[web.1]: TypeError: Cannot convert greenlet.greenlet to gevent._gevent_c_greenlet_primitives.SwitchOutGreenletWithLoop
2020-07-09T14:55:03.812531+00:00 heroku[router]: at=info method=POST path="/socket.io/?EIO=3&transport=polling&t=NCq5C0_&sid=59a6f168828134cc39e7314cf4639909f" host=chat-py.herokuapp.com request_id=9c21cf77-3a08-44b9-917b-da87c2dad3fc fwd="176.229.186.130" dyno=web.1 connect=2ms service=7ms status=200 bytes=202 protocol=http
2020-07-09T14:55:33.809904+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/socket.io/?EIO=3&transport=polling&t=NCq5C13&sid=59a63f16888134cc39e7314cf4639909f" host=chat-py.herokuapp.com request_id=5ca46303-e131-4a64-844a-0070916051dc fwd="176.229.186.130" dyno=web.1 connect=1ms service=30000ms status=503 bytes=0 protocol=http
2020-07-09T14:55:33.920874+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/socket.io/?EIO=3&transport=websocket&sid=59a6f16888234cc39e7314cf4639909f" host=chat-py.herokuapp.com request_id=87ec71b4-c64a-4e3a-bf31-c6fda30eda3f2 fwd="176.229.186.130" dyno=web.1 connect=2ms service=30000ms status=503 bytes=0 protocol=http

我可以看到该站点 运行 但由于某种原因,连接到网络选项卡中的 socket-io 时出现问题(您无法发送消息,但它在 localhost 中有效)。

网络选项卡

Pipfile:

flask = "*"
flask-sqlalchemy = "*"
flask-socketio = "*"
flask-login = "*"
sqlalchemy = "*"
gunicorn = "*"
psycopg2 = "*"
eventlet = "*"
gevent-websocket = "*"

Procfile:

web: gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -w 1 app:app

(服务器名称为app)

connecting to socketio:

<script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js" integrity="hidden" crossorigin="anonymous"></script>

start of the frontend code (JS):

$(document).ready(function(){
      var socket = io("http://chat-py.herokuapp.com:" + location.port);
.....

Heroku logs

remote:  !     Python has released a security update! Please consider upgrading to python-3.8.3
remote:        Learn More: https://devcenter.heroku.com/articles/python-runtimes
remote: -----> No change in requirements detected, installing from cache
remote: -----> Installing dependencies with Pipenv 2018.5.18…
remote:        Installing dependencies from Pipfile.lock (570f04)…
remote:        Ignoring cffi: markers 'platform_python_implementation == "CPython" and sys_platform == "win32"' don't match your environment
remote:        You are using pip version 9.0.2, however version 20.1.1 is available.
remote:        You should consider upgrading via the 'pip install --upgrade pip' command.

UPDATED network tab:

在使用 cors_allowed_origins 选项配置后,我遇到了一个错误(我很确定错误的原因在日志中):

好的,所以我不确定是什么修复了它,但我会告诉你我在推送中所做的工作,我重新安装了 gunicorn(实际上安装了多次)并升级了 python从 3.8.2 到 3.8.3(通过将 runtime.txt 从 python-3.8.2 更改为 python-3.8.3)。在我将它推送到 heroku 并进入该站点后,它就可以正常工作了。