我正在尝试制作使用WebSockets的Flask应用。 example from Flask-sockets有效,但我如何从常规视图发送消息?
与Flask-SocketIO如何使用.emit()
和.send()
- 方法类似。
在下面的示例中(来自Flask-Sockets示例)我希望能够从hello
- 视图广播消息。
from flask import Flask
from flask_sockets import Sockets
app = Flask(__name__)
sockets = Sockets(app)
@sockets.route('/echo')
def echo_socket(ws):
while not ws.closed:
message = ws.receive()
ws.send(message)
@app.route('/')
def hello():
# How can I send a WebSocket message from here?
return 'Hello World!'
if __name__ == "__main__":
from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
server = pywsgi.WSGIServer(('', 5000), app, handler_class=WebSocketHandler)
server.serve_forever()