Flask: A RESTful API and SocketIO Server

前端 未结 1 868
灰色年华
灰色年华 2020-12-28 08:31

Background

I am trying to create a simple REST API using the Flask-RESTful extension. This API will be working primarily to manage the CRUD and auth

相关标签:
1条回答
  • 2020-12-28 08:42

    You just want to run socketio.run(app, port=5005) and hit the REST API on port 5005.

    The reason this works is because under the hood, Flask-SocketIO is running an evented webserver based on gevent (or with the 1.0 release, also eventlet) - this server handling the websocket requests directly (using the handlers you register via the socketio.on decorator) and is passing on the non-websocket requests to Flask.

    The reason your code wasn't working is because both app.run and socketio.run are blocking operations. Whichever one ran first was looping, waiting for connections, never allowing the second to kick off. If you really needed to run your websocket connections on a different port you'd need to spawn either the socketio or the app run call on a different process.

    0 讨论(0)
提交回复
热议问题