[英]Continuously sending data from Python Flask server to client through Websockets
I am setting up a dashboard which requires a Python Flask server to fetch public transport data once every few seconds or so and send this data to a client webpage through websockets.我正在设置一个仪表板,它需要一个 Python Flask 服务器每隔几秒左右获取一次公共交通数据,并通过 websockets 将此数据发送到客户端网页。 I am using FlaskSocketIO to achieve this.
我正在使用 FlaskSocketIO 来实现这一点。
I am able to send a message upon the 'connect'
event, for example, but I've been unable to find a way to continuously update this message.例如,我可以在
'connect'
事件时发送消息,但我一直无法找到一种方法来持续更新此消息。 This is my approach:这是我的方法:
from flask import Flask, render_template, url_for, request
from flask_socketio import SocketIO, emit, send
import eventlet
eventlet.monkey_patch()
app = Flask(__name__)
socketio = SocketIO(app, logger=True, engineio_logger=True)
def listen():
while True:
message = # some updating message
emit('message', message)
socketio.sleep(1)
@app.route('/')
def index():
return render_template('index.html')
@socketio.on('connect')
def handle_connect():
listen()
if __name__ == "__main__":
socketio.run(app, debug=True)
The logger says it sends the message.记录器说它发送了消息。 However, on the client side, no message is recieved.
但是,在客户端,没有收到任何消息。 The client side JavaScript is:
客户端 JavaScript 是:
var socket = io();
socket.on('message', function(msg) {
console.log(msg);
});
i have the same problem with you, in my case i wanted to update sensor dashboard, so i study about Observer Pattern, this link is really good Observer Pattern .Implement a SocketEmitObservator, using the Observer you dont need to create a listener(infinite while loop) and make your code more clean:我和你有同样的问题,在我的情况下,我想更新传感器仪表板,所以我研究了观察者模式,这个链接是非常好的观察者模式。实现一个 SocketEmitObservator,使用你不需要创建监听器的观察者(无限时)循环)并使您的代码更干净:
class SensorObserver(Observer):
sensor_id: int
sid = ''
def __init__(self, sid,sensor_id=None):
Observer.__init__(self)
self.sensor_id = sensor_id
self.sid = sid
SensorPublisher().attach(self)
def update(self, sensorData: DataModel) -> None:
socket.emit("info",
{'sensor': subject},
namespace='/api/sector/machine',
room=self.sid)
def __del__(self):
SensorPublisher().detach(self)
class SensorPublisher(Subject, Singleton):
_observers: List[SensorObserver] = []
_sensorData: Dict[str, DataModel] = {}
def attach(self, observer: SensorObserver) -> None:
if observer not in self._observers:
self._observers.append(observer)
def notify(self, sensorModel: DataModel) -> None:
for observer in self._observers:
if sensorModel.sensor_id == observer.sensor_id:
observer.update(sensorModel)
def detach(self, observer: SensorObserver) -> None:
if observer in self._observers:
ob = self._observers.pop(self._observers.index(observer))
del ob
def update(self, sensorModel: DataModel):
sendorId: str = str(sensorModel.sensor_id)
self._sensorData[sendorId] = sensorModel
self.notify(sensorModel)
#### And in the place where i update sensor data i put
SensorPublisher().update(dataStructList)
#### To create a observr just do
@socketio.on('connect')
def handle_connect():
SensorObserver(self.sid, item)
In my case i use like this, but you can adapt for your problem在我的情况下,我是这样使用的,但您可以适应您的问题
You cannot add a while True
loop in the connect handler, as that will prevent the connect handler from returning and establishing the connection.您不能在连接处理程序中添加
while True
循环,因为这会阻止连接处理程序返回并建立连接。
You can do this as a response to another event sent by the client after the connection is made, or you can also start a background task that implements this loop.您可以将此作为对建立连接后客户端发送的另一个事件的响应,或者您也可以启动一个实现此循环的后台任务。 The connect handler needs to return quickly to allow the connection to happen.
连接处理程序需要快速返回以允许连接发生。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.