classes.py
)发出socketio消息? classes.py
已了解socketio
变量的相关信息,即使尝试直接导入,也无效。
请帮我解决我的问题!
这是我的代码;
的app.py
from flask import Flask, render_template, redirect, url_for, request, session, send_from_directory, jsonify
from flask_socketio import SocketIO
from classes import *
app = Flask(__name__)
socketio = SocketIO(app, message_queue='redis://localhost:6379')
if __name__ == "__main__":
app.debug = True
socketio.run(app, port=5000, debug=True, use_reloader=True)
的 classes.py
from flask import Flask, request, session, redirect, url_for, jsonify
import requests, json, random, sqlite3
from flask_socketio import SocketIO
class notify:
def __init__(self, message=None):
socketio.emit('notification', {'message': message})
@app.route()
发送POST请求,触发socketio.emit()
答案 0 :(得分:3)
似乎有一个循环引用。
您应该从入口点划分app
变量。
app.py
from flask import Flask, render_template, redirect, url_for, request, session, send_from_directory, jsonify
from flask_socketio import SocketIO
app = Flask(__name__)
socketio = SocketIO(app, message_queue='redis://localhost:6379')
classes.py
from app import socketio
class notify:
def __init__(self, message=None):
socketio.emit('notification', {'message': message})
main.py
from app import app, socketio
from classes import *
if __name__ == "__main__":
app.debug = True
socketio.run(app, port=5000, debug=True, use_reloader=True)