简体   繁体   English

烧瓶套接字 io heroku 超时

[英]flask socket io heroku timeout

I have a js socket.io client that only connects to my flask socket.io when i run it locally, but it does not connect when i try through heroku.我有一个 js socket.io 客户端,当我在本地运行它时,它只连接到我的烧瓶 socket.io,但是当我尝试通过 heroku 时它没有连接。 the other parts of the flask api work and run good, the only thing that does not work is socket.io connection.烧瓶 api 的其他部分工作并运行良好,唯一不工作的是 socket.io 连接。 Their is aconsole logs "disconnected", sometimes also a error in the console of Uncaught (in promise) Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received , even though the connection is not requested to do something.他们是控制台日志“断开连接”,有时在Uncaught (in promise) Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received ,即使连接未要求做某事。
this is my html js socket.io client code:这是我的 html js socket.io 客户端代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>socket</title>
</head>
<body>
    <h1>Socket.IO Demo</h1>
    <button onclick="send()">sdgfhk</button>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/3.0.4/socket.io.js" integrity="sha512-aMGMvNYu8Ue4G+fHa359jcPb1u+ytAF+P2SCb+PxrjCdO3n3ZTxJ30zuH39rimUggmTwmh2u7wvQsDTHESnmfQ==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.5.0/socket.io.min.js"></script>
    <script>
        const socket = io.connect("https://onlineauctionapi.herokuapp.com/", { transports: ['websocket'] });
        socket.on("connect_error", (err) => {
        console.log(`connect_error due to ${err.message}`);
        });


        socket.on('join', () => {
            console.log("joined")
        });

        function send(){
            socket.emit('send',{message:"hello", room:"hello"})
        }

        socket.on('connect', () => {
            console.log("connected")
        });

        socket.on('disconnect', () => {
            console.log('disconnected');
        });
    </script>
</body>
</html>

this is my flask socket.io server code:这是我的烧瓶 socket.io 服务器代码:

app = Flask(__name__)
CORS(app, resources={r"/api/*":{"origins":"*"}})
socketio = SocketIO(app, cors_allowed_origins="*")

@app.route('/signin', methods=['POST'])
def signIn():
    return signin(request) 
# signin is a function that signs you in

@socketio.on('connect')
def on_connect():
    print("connected")


if __name__ == '__main__':
    socketio.run(app, int(os.environ.get('PORT', 5000)), debug=True)

this is my Procfile:这是我的 Procfile:

web: gunicorn main:app

this is my requirements.txt这是我的要求.txt

bidict==0.22.0
cffi==1.15.0
click==8.1.3
colorama==0.4.5
dnspython==2.2.1
eventlet==0.33.1
Flask==2.1.2
Flask-Cors==3.0.10
Flask-SocketIO==5.2.0
gevent==21.12.0
gevent-websocket==0.10.1
gitdb==4.0.9
GitPython==3.1.27
greenlet==1.1.2
gunicorn==20.1.0
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.1
pycparser==2.21
pymongo==4.1.1
python-dateutil==2.8.2
python-dotenv==0.20.0
python-engineio==4.3.2
python-socketio==5.6.0
six==1.16.0
smmap==5.0.0
Werkzeug==2.1.2
zope.event==4.5.0
zope.interface==5.4.0

I tried playing around with the versions of the socket in the flask api, but it did not help.我尝试使用烧瓶 api 中的套接字版本,但没有帮助。 the regular requests and the socket.io requests are on the same api, and still the regular request are working, but the socket ones are not.常规请求和 socket.io 请求在同一个 api 上,并且常规请求仍然有效,但套接字请求没有。

these are my logs from heroku:这些是我来自heroku的日志:

2022-06-28T15:41:18.631530+00:00 app[web.1]: connected
2022-06-28T15:41:49.062385+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=4&transport=websocket" host=onlineauctionapi.herokuapp.com request_id=9a63da54-26fd-4c89-a96c-f21405cd08fe fwd="46.19.85.189" dyno=web.1 connect=0ms service=30598ms status=101 bytes=202 protocol=https
2022-06-28T15:41:49.061409+00:00 app[web.1]: [2022-06-28 15:41:49 +0000] [4] [CRITICAL] WORKER TIMEOUT (pid:40)
2022-06-28T15:41:49.061951+00:00 app[web.1]: [2022-06-28 15:41:49 +0000] [40] [INFO] Worker exiting (pid: 40)
2022-06-28T15:41:49.244258+00:00 app[web.1]: [2022-06-28 15:41:49 +0000] [42] [INFO] Booting worker with pid: 42

Since you have the eventlet & gevent libraries, your socketio.run(...) would have been enough for socket.io to work according to the docs :由于您拥有eventletgevent库,因此您的socketio.run(...)就足以让 socket.io 根据文档工作:

If eventlet or gevent are available, socketio.run(app) starts a production-ready server using one of these frameworks.如果 eventlet 或 gevent 可用,socketio.run(app) 使用这些框架之一启动生产就绪服务器。

However, you're deploying your app in heroku using Gunicorn as your web server, which requires additional commands to start eventlet or gevent .但是,您正在使用 Gunicorn 作为 Web 服务器在 heroku 中部署您的应用程序,这需要额外的命令来启动eventletgevent You have 2 options - both requiring update in your Procfile :您有 2 个选项 - 都需要在您的Procfile中更新:

  1. Using eventlet使用eventlet
web: gunicorn --worker-class eventlet -w 1 main:app
  1. Using gevent使用gevent
web: gunicorn -k gevent -w 1 main:app

You can read more about this here .您可以在此处阅读有关此内容的更多信息。 Also, ensure that your socketio.run(...) does not have local settings.此外,请确保您的socketio.run(...)没有本地设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM