我正在构建一个小型烧瓶&基于pyhon的应用程序,我的主要功能是基于websockets。我发现我无法修改websockets事件的事件处理程序中的sesssion的值(我使用flask-socketio
),因为flask将其会话存储在客户端。所以,作为扩展名recomanded的作者,我安装了flask-kvsession
来将该会话存储在基于redis
的后端的服务器端。无法修改事件处理程序中的websockets事件会话的值
我按照提示http://pythonhosted.org/Flask-KVSession/,但问题依然存在。所以我创建了一个小程序来向你展示我在说什么。
# main.py
from flask import Flask, session, render_template
from flask.ext.socketio import SocketIO
from pprint import pprint
import redis
from flask_kvsession import KVSessionExtension
from simplekv.memory.redisstore import RedisStore
store = RedisStore(redis.StrictRedis())
app = Flask(__name__)
app.debug = True
app.secret_key = 'secret!'
KVSessionExtension(store, app)
socketio = SocketIO(app)
@app.route('/')
def index():
pprint(session)
return render_template("client.html")
@socketio.on('connect')
def handle_connect(message):
session['debug'] = 'debug'
pprint(session)
if __name__ == "__main__":
socketio.run(app)
<!-- templates/client.html -->
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/
socket.io/0.9.16/socket.io.min.js"></script>
<script type="text/javascript">
var sock = io.connect('http:localhost:5000');
sock.emit('connect', {debug: 'debug'});
</script>
</body>
</html>
这里是WERKZEUG调试运行服务器的输出:
* Running on http://127.0.0.1:5000/
* Restarting with reloader
<KVSession {}>
127.0.0.1 - - [2014-07-04 21:25:51] "GET/HTTP/1.1" 200 442 0.004452
<KVSession {'debug': 'debug'}>
<KVSession {}>
127.0.0.1 - - [2014-07-04 21:26:02] "GET/HTTP/1.1" 200 442 0.000923
<KVSession {'debug': 'debug'}>
我想到的是,第二遍的时候,我会访问该网页的会话内容是'debug': 'debug'
但它不是。
这里是Redis的服务器上发生的事情,而我是运行这个程序:
127.0.0.1:6379> MONITOR
OK
1404498351.888321 [0 127.0.0.1:38129] "GET" "136931c509f674e3_53b6e25b"
1404498352.073011 [0 127.0.0.1:38129] "GET" "136931c509f674e3_53b6e25b"
1404498362.455320 [0 127.0.0.1:38129] "GET" "136931c509f674e3_53b6e25b"
1404498362.612346 [0 127.0.0.1:38129] "GET" "136931c509f674e3_53b6e25b"
正如你所看到的,会议的价值被访问4次,但从未modfied。 那么,我该如何解决这个错误?