0
有沒有基於外部事件觸發send()
websocket命令的方法?每次數據庫更新時,我都試圖推送給客戶端。我使用SQL notify
,一個uwsgi文件監視器裝飾等基本代碼試圖爲從外部事件觸發Flask中的WebSocket
from flask.ext.uwsgi_websocket import GeventWebSocket
from uwsgidecorators import *
ws = GeventWebSocket(app)
@ws.route('/feed')
def socket(ws):
ws_readystate = ws.receive()
if ws_readystate == '1':
ws.send(json.dumps('this message is received just fine'))
# client is ready, start something to trigger sending a message here
@filemon("../mydb.sqlite")
def db_changed(x):
print 'DB changed'
ws.send(json.dumps('db changed'))
這將打印輸出「DB改變」,但客戶不會收到了「DB改變」消息。我運行的應用程序作爲
uwsgi --master --http :5000 --http-websockets --gevent 2 --wsgi my_app_name:app