【问题标题】:Unable to pass button event to Flask SocketIO to external python program无法将按钮事件传递给 Flask SocketIO 到外部 python 程序
【发布时间】:2018-09-29 18:00:56
【问题描述】:

我需要将我的 python Flask Web 应用程序和通过 socketio 通信的运行代码分开。我能够从外部 python 程序获取消息到网络,但我无法从 python 程序检测到的网络获取事件。实际上,我希望当用户按下网页上的按钮时,外部 python 代码将打印到终端一个 hello world 消息。在这个 html 的控制台中,我肯定会看到“按下按钮”。

<html>
<head>
<title>Listener</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.8/socket.io.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function() {
    var socket = io.connect();

    $('#mybutton').on('click', function() {
        socket.emit('my event', 'yodle');
        console.log('Button pressed');
    });
});
</script>
<button id="mybutton">Push Me!</button>
</body>
</html>

这是我的基本 Flask 网络服务器代码,它正在工作:

from flask import Flask, render_template
from flask_socketio import SocketIO

app = Flask(__name__)
app.config['SECRET_KEY'] = 'froggy'
app.debug = True
socketio = SocketIO(app, message_queue='redis://')

@app.route("/")
def index():
  return render_template("index.html")

if __name__ == '__main__':
    socketio.run(app, host='0.0.0.0')

这是我单独运行的程序,按下按钮时不会向终端打印任何内容:

from flask_socketio import SocketIO

socketio = SocketIO(message_queue='redis://', host='0.0.0.0')

def my_function_handler(data):
    print("Hello World")

if __name__ == '__main__':
    while True:
        socketio.on_event('my event', my_function_handler)

谁能指出我哪里出错了?非常感谢!

【问题讨论】:

    标签: python socket.io flask-socketio


    【解决方案1】:

    您正在尝试执行不受支持的操作。外部进程只能发出,它们不是接收者。如果你需要在外部进程中发送和接收,那么我建议你将 Socket.IO 服务器完全移到这个进程中。

    【讨论】:

      【解决方案2】:

      显然,根据 Miguel 的回答,socketIO 不支持这种行为,所以如果有人感兴趣,我将发布我对该问题的特定解决方案。我花了很多时间在谷歌上搜索、堆栈溢出,并且通常会掉进兔子洞。我最终做的是,因为我已经在使用 redis,所以我只是用它在进程之间传递消息。这种方法有很多普遍的缺点,但它很好地满足了我的需求,所以这就是我所做的。对于 index.html:

      <html>
      <head>
      <title>Listener</title>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.8/socket.io.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
      </head>
      <body>
      <script type="text/javascript">
      $(document).ready(function() {
          var socket = io.connect();
      
          $('#mybutton').on('click', function() {
              socket.emit('button event');
          });
      });
      </script>
      <button id="mybutton">Push Me!</button>
      </body>
      </html> 
      

      服务器代码:

      from flask import Flask, render_template
      from flask_socketio import SocketIO
      import redis
      
      app = Flask(__name__)
      app.config['SECRET_KEY'] = 'froggy'
      app.debug = True
      socketio = SocketIO(app, message_queue='redis://')
      
      r = redis.Redis("localhost")
      r.set('button', 'not pressed')
      
      @app.route("/")
      def index():
        return render_template("index.html")
      
      @socketio.on('button event')
      def handleMessage():
          r.set('button', 'pressed')
      
      if __name__ == '__main__':
          socketio.run(app, host='0.0.0.0')
      

      单独运行进程:

      import redis
      
      r = redis.Redis("localhost")
      
      if __name__ == '__main__':
          while True:
              if r.get('button') == 'pressed':
                  print("Button pressed!")
                  r.set('button', 'not pressed')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-27
        • 2021-05-30
        • 1970-01-01
        相关资源
        最近更新 更多