使用Flask-MQTT和Flask-SocketIO,我试图在接收到MQTT消息时,从Flask-SocketIO事件上下文向所有连接的客户端发出套接字消息。 但是,这样做时,消息似乎永远不会到达客户端。
看来这是long known issue,但使用app.app_context()
似乎也不适合我。
这是我现在拥有的:
app = Flask(__name__)
[app.config ...]
cors = CORS(app,resources={ "/*": {"origins":"*"} })
mqtt = Mqtt(app)
socketio = SocketIO(app)
[...]
@mqtt.on_message()
def handle_mqtt_message(client, userdata, message):
with app.app_context():
# Send socket to all connected clients.
# Not working for some reasons...
socketio.emit("mqtt_message")
@socketio.on("connect")
def handle_socketio_connect():
# This works.
emit("hello")
if __name__ == "__main__":
socketio.run(app, [...] )
软件包版本: