gpt4 book ai didi

python - 从线程发出时,Flask Socket.io 不起作用

转载 作者:行者123 更新时间:2023-12-04 15:46:27 49 4
gpt4 key购买 nike

描述

客户端有2个按钮:

  • 一个让服务器定期发送消息
  • 另一个,停止发送周期性消息

这个问题代表了我要解决的真正问题。

我构建了应用程序,在服务器端它似乎工作正常,但客户端没有收到服务器推送,但能够启动推送并将其终止!

我尝试了什么

服务器端

import random
from threading import Thread
from time import sleep

from flask import Flask
from flask_socketio import SocketIO

SOCKET_NAMESPACE = '/test'

is_pushing = False

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!!'
socketio = SocketIO(app)


def server_push(fps):
dt = 1 / fps
global is_pushing
while is_pushing:
with app.test_request_context('/'):
sent = f"Server Pushed!! {random.random()}"
print(sent)
socketio.emit("serverResponse", sent, namespace=SOCKET_NAMESPACE)
sleep(dt)


@socketio.on('connect', namespace=SOCKET_NAMESPACE)
def on_connect():
print("connected server!!")
socketio.emit("serverResponse", "First Push", namespace=SOCKET_NAMESPACE)


@socketio.on('disconnect', namespace=SOCKET_NAMESPACE)
def on_disconnect():
print("disconnected server!!")


@socketio.on('startServerPush', namespace=SOCKET_NAMESPACE)
def on_start_server_push(fps=1):
print("Sever push start!!")
global is_pushing
is_pushing = True
socketio.emit("serverResponse", "Start Push", namespace=SOCKET_NAMESPACE)
Thread(target=lambda: server_push(fps)).start()


@socketio.on("killServerPush", namespace=SOCKET_NAMESPACE)
def on_kill_server_push():
print("Server push stop!!")
global is_pushing
is_pushing = False
socketio.emit("serverResponse", "Kill Push", namespace=SOCKET_NAMESPACE)


def main():
socketio.run(app, port=8082, debug=True)


if __name__ == '__main__':
main()

客户端

import openSocket from 'socket.io-client';
import React, { Component } from 'react';

class Test extends Component {
state = {
pushedFromServer: [],
socket: null
};

componentDidMount() {
const url = 'localhost:8082/test';
const socket = openSocket(url);
socket.on('connect', () => console.log('Test connected!!'));
socket.on('disconnect', () => console.log('Test disconnected!!'));
socket.on('serverResponse', response => {
console.log(response);
const pushedFromServer = [...this.state.pushedFromServer];
pushedFromServer.push(response);
this.setState({ pushedFromServer });
});
this.setState({ socket });
}

killServerPush = () => {
this.state.socket.emit('killServerPush');
};

startServerPush = () => {
this.state.socket.emit('startServerPush');
};

render() {
return (
<div>
<button onClick={this.startServerPush}>
<h3>Start push from server</h3>
</button>
<button onClick={this.killServerPush}>
<h3>Kill push from server</h3>
</button>
<ul>
{this.state.pushedFromServer.map(value => (
<li>{value}</li>
))}
</ul>
</div>
);
}
}

export default Test;

最后的笔记

在客户端中,我可以接收到First PushStart Push,我还可以从客户端停止周期性进程并重新启动它。 我无法在客户端接收定期消息

谢谢

最佳答案

通过查看 https://github.com/miguelgrinberg/python-socketio/issues/99 ,我找到了解决问题的方法。

只需更改服务器端即可。

换行:

Thread(target=lambda: server_push(fps)).start()

 socketio.start_background_task(target=lambda: server_push(fps))

而不是使用 python sleep,使用:

socketio.sleep(dt)

关于python - 从线程发出时,Flask Socket.io 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55572868/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com