【发布时间】:2015-06-26 15:37:33
【问题描述】:
所有,我正在使用烧瓶/uWSGI 实现 websocket。这归结为在主应用程序中实例化的模块。服务器和模块的编辑代码:
main.py
from WSModule import WSModule
app = Flask(__name__)
wsmodule = WSModule()
websock = WebSocket(app)
@websock.route('/websocket')
def echo(ws):
wsmodule.register(ws)
print("websock clients", wsmodule.clients)
while True: # This while loop is related to the uWSGI websocket implementation
msg = ws.receive()
if msg is not None:
ws.send(msg)
else: return
@app.before_request
def before_request():
print ("app clients:",wsmodule.clients)
和WSModule.py:
class WSModule(object):
def __init__(self):
self.clients = list()
def register(self, client):
self.clients.append(client)
问题:当用户使用 websockets 连接时(进入 '/websocket' 路由),wsmodule.register 会附加他们的连接套接字,这可以正常工作 - 打印输出 'websocket clients' 显示附加的连接. 问题是我无法从主应用程序访问这些套接字。从永远不会更新的“应用程序客户端”打印输出中可以看到这一点(列表保持为空)。某些内容明显在更新,但如何访问这些更改?
【问题讨论】:
标签: python-3.x flask