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

Flask Socketio:WebSocket握手期间出错:意外响应代码:503&;在建立连接之前,WebSocket已关闭,flask,heroku,websocket,socket.io,flask-socketio,Flask,Heroku,Websocket,Socket.io,Flask Socketio,所以,我正在尝试将我的聊天应用程序部署到Heroku上。我看到了一些答案,但没有一个是与flask socketio相关的,它们都是与flask socketio相关的解决方案,而这些解决方案是在flask socketio中无法实现的。我跟着脚步走了进去。此外,您还可以发现日志中存在错误 2020-07-09T14:55:03.811700+00:00 app[web.1]: Traceback (most recent call last): 2020-07-09T14:55:03.8117

所以,我正在尝试将我的聊天应用程序部署到Heroku上。我看到了一些答案,但没有一个是与flask socketio相关的,它们都是与flask socketio相关的解决方案,而这些解决方案是在flask socketio中无法实现的。我跟着脚步走了进去。此外,您还可以发现日志中存在错误

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
我可以看到该站点已启动并正在运行,但由于某些原因,连接到“网络”选项卡中的套接字io时出现问题(您无法发送消息,并且它在localhost中工作)

网络选项卡

文件:

程序文件:

(服务器名称为app)

连接到socketio:

Heroku原木

更新的网络选项卡:

在使用cors_allowed_origins选项配置后,我只剩下一个错误(我非常确定错误的原因在日志中):

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

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

查看浏览器的“网络”选项卡,查看客户端和服务器之间的通信情况。@Miguel是的,我忘记了。更新了问题400错误可能是一个跨源问题,如果你没有采取任何措施来阻止它。这是因为Flask应用程序没有s不知道Heroku在哪个域上安装了它。请参阅
cors\u allowed\u origins
选项,将您的域配置为允许的源。503个错误我不确定,它们不是来自应用程序。@Miguel好的,我在上面。为什么在建立连接之前关闭websocket?这是因为400错误吗/503错误?对,错误发生在连接请求中因此,您永远无法完成连接尝试。查看浏览器的“网络”选项卡,查看客户端和服务器之间的通信情况。@Miguel是的,我忘记了它。更新了问题400错误可能是跨源问题,如果您没有采取任何措施阻止它。这是因为Flask应用程序不知道域n Heroku放的。请参阅
cors\u allowed\u origins
选项,将您的域配置为允许的源。503错误我不确定,它们不是来自应用程序。@Miguel好的,我在上面。为什么在建立连接之前关闭websocket?是因为400/503错误吗?是的,是的连接请求中发生错误,因此您永远无法完成连接尝试。
flask = "*"
flask-sqlalchemy = "*"
flask-socketio = "*"
flask-login = "*"
sqlalchemy = "*"
gunicorn = "*"
psycopg2 = "*"
eventlet = "*"
gevent-websocket = "*"
web: gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -w 1 app:app
<script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js" integrity="hidden" crossorigin="anonymous"></script>
$(document).ready(function(){
      var socket = io("http://chat-py.herokuapp.com:" + location.port);
.....
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.