我使用了来自http://blog.miguelgrinberg.com/post/easy-websockets-with-flask-and-gevent/page/4的以下示例代码,当我使用测试服务器运行它时它工作正常,例如python myapp.py
我可以连接到它并发送消息
from flask import Flask, render_template
from flask.ext.socketio import SocketIO, emit
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
@app.route('/')
def index():
return render_template('index.html')
@socketio.on('my event', namespace='/test')
def test_message(message):
emit('my response', {'data': message['data']})
@socketio.on('my broadcast event', namespace='/test')
def test_message(message):
emit('my response', {'data': message['data']}, broadcast=True)
@socketio.on('connect', namespace='/test')
def test_connect():
emit('my response', {'data': 'Connected'})
@socketio.on('disconnect', namespace='/test')
def test_disconnect():
print('Client disconnected')
if __name__ == '__main__':
socketio.run(app)
问题是当我将相同的代码移动到使用 Apache 为 Flask 提供服务的服务器时,我得到了错误。
RuntimeError: You need to use a gevent-socketio server.
Apache 主机的配置文件是:
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /var/www/public/flaskApp/flaskApp.wsgi
<Location /var/www/public/flaskApp/flaskApp/>
Order allow,deny
Allow from all
</Location>
是否可以运行 SocketIO/Flask 并通过 Apache 运行?