Flask 带数据库通信的socketio

Flask 带数据库通信的socketio,flask,websocket,socket.io,flask-socketio,Flask,Websocket,Socket.io,Flask Socketio,我想做的是下一步: 第一次连接由客户进行(“连接”) 然后从Python中调用函数,该函数根据数据库中的信息,将emmit转换为另一个“套接字”(test) 但我的问题是,当数据库中有任何寄存器时,它会生成一个emmit来测试并显示“Youhavesomething!”,但当我从数据库中删除信息而数据库中没有寄存器时,这就不起作用了。我必须手动重新刷新pade,使其显示“你什么都没有!”!“。有没有更好的方法?不重新刷新页面?这是一个全双工。提前谢谢 Python: from flask imp

我想做的是下一步:

  • 第一次连接由客户进行(“连接”)
  • 然后从Python中调用函数,该函数根据数据库中的信息,将emmit转换为另一个“套接字”(test)
  • 但我的问题是,当数据库中有任何寄存器时,它会生成一个emmit来测试并显示“Youhavesomething!”,但当我从数据库中删除信息而数据库中没有寄存器时,这就不起作用了。我必须手动重新刷新pade,使其显示“你什么都没有!”!“。有没有更好的方法?不重新刷新页面?这是一个全双工。提前谢谢

    Python:

    from flask import Flask, render_template
    from flask_socketio import SocketIO, emit, send
    from flask_cors import CORS
    import MySQLdb
    
    app = Flask(__name__)
    app.config['SECRET_KEY'] = 'secret!'
    socketio = SocketIO(app)
    CORS(app)
    
    @socketio.on('connect')
    def con():
        handle_message()
    
    def handle_message():
        db = MySQLdb.connect("localhost","root","","sitandorder" )
        cursor = db.cursor()
        cursor.execute("SELECT * FROM historico_pedido")
        data = cursor.fetchall()
        print(len(data))
        if len(data) >= 1:
            emit ("test" ,"You have something!")
        else:
            emit ("test" ,"You have nothing!")
        db.close()
    
    
    if __name__ == '__main__':
        socketio.run(app, debug=True, host='127.0.0.1', port=5000 )
    
    Javascript:

    <!DOCTYPE html>
    <html lang="en" dir="ltr">
      <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js">
        </script>
        <meta charset="utf-8">
        <title></title>
      </head>
      <body>
        <script type="text/javascript">
          $(document).ready(function(){
              var socket = io.connect('http://127.0.0.1:5000');
              socket.on('connect', function(){
                console.log("Connected"); // Make the connection!
              });
              socket.on('test', function(arg){
                console.log(arg); //Here i show what i get from the handle_message() of python
              })
          });
        </script>
      </body>
    </html>
    
    
    $(文档).ready(函数(){
    var socket=io.connect('http://127.0.0.1:5000');
    socket.on('connect',function(){
    console.log(“已连接”);//建立连接!
    });
    socket.on('test',函数(arg){
    console.log(arg);//这里我展示了从python的handle_message()中获得的内容
    })
    });
    
    在循环中的单独线程中执行
    处理\u消息
    ,并将数据发送到客户端可能是一种选择

    这是一个基本的想法。看看

    @socketio.on('connect')
    def con():
        thread = Thread(target=background_task)
        thread.start()
    
    def background_task():
        while True:
           time.sleep(1)
           handle_message()
    
    
    def handle_message():
        db = MySQLdb.connect("localhost","root","","sitandorder" )
        cursor = db.cursor()
        cursor.execute("SELECT * FROM historico_pedido")
        data = cursor.fetchall()
        print(len(data))
        if len(data) >= 1:
            emit ("test" ,"You have something!")
        else:
            emit ("test" ,"You have nothing!")
        db.close()