简体   繁体   English

通过Websockets从Python Flask服务器向客户端持续发送数据

[英]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.

相关问题 将数据从Python TCP套接字(作为服务器)发送到JS客户端 - Sending data from the Python TCP Socket(as server) to the JS client 无法通过flask-socketIO服务器在HTML客户端中显示来自python客户端的视频流 - Cant display my video stream from a python client in the HTML client through a flask-socketIO server 从Python(Flask)发送数据到Javascript? - Sending Data from Python (Flask) to Javascript? 通过Flask发送POST数据 - Sending POST Data through Flask 使用 Websockets 从 Python 程序连续输出流 - Continuously stream output from program in Python using Websockets 将数据从html发送到pythonflask服务器“GET/HTTP/1.1”405错误 - Sending data from html to python flask server "GET / HTTP/1.1" 405 error 通过节点 js 将响应从 python flask 转发到客户端 - forwarding a response from python flask to client through node js 初始握手后,如何在 Python WebSockets 服务器中将消息从服务器发送到客户端? - How to send messages from server to client in a Python WebSockets server, AFTER initial handshake? Websocket向服务器发送垃圾 - Websockets sending garbage to server 如何使用Python(带有Websocket的服务器)和JavaScript(客户端)接收JSON数据 - How to receive JSON data with Python (server with websockets) and JavaScript (client-side)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM