Flask SocketIO 消息未收到
Posted
技术标签:
【中文标题】Flask SocketIO 消息未收到【英文标题】:Flask SocketIO messages not received 【发布时间】:2019-09-25 10:53:32 【问题描述】:我是 Flask 和 IO 的新手。我正在尝试为具有 RoS 的机器实现基本数据接收器。我有一个 python 脚本将数据发送到在 Flask 上运行的 Web 服务器。 问题是从未调用过 java-script 回调,因此奇怪的是,即使没有错误,这些数字也从未添加到列表中。 相关部分是这样的:
在 Python 网络服务器中:
@socketio.on('connect', namespace='/test')
def test_connect():
print("CALLED")
@socketio.on('disconnect', namespace='/test')
def test_disconnect():
print('Client disconnected')
它应该发布一个由以下 Ros 回调函数定义的随机数(每 0.5 秒调用一次):
def forklift_callback(self, msg):
#SOCKET TEST
number = random.randint(1,101)
print(number)
socketio.emit('newnumber', 'number': number, namespace='/test')
也许我定义它的方式很重要:
*import statements*
from std_msgs.msg import String, UInt32
from umd_msgs.msg import PCNotification, RoverStatus, Barcode
mod = Blueprint('api', __name__)
msg = String()
started = "robot started"
socketio = SocketIO(app)
... All functionality ...
if __name__ == '__main__':
socketio.run(app)
这是连接到以下位置的 html 页面:
<html>
<head>
<script src="//code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.6/socket.io.min.js"></script>
<script>
$(document).ready(function()
//connect to the socket server.
console.log('http://' + document.domain + ':' + location.port + '/test')
var socket = io.connect('http://' + document.domain + ':' + location.port + '/test');
var numbers_received = [];
//receive details from server
socket.on('newnumber', function(msg)
console.log("Blank")
console.log("Received number" + msg.number);
//maintain a list of ten numbers
if (numbers_received.length >= 10)
numbers_received.shift()
numbers_received.push(msg.number);
numbers_string = '';
for (var i = 0; i < numbers_received.length; i++)
numbers_string = numbers_string + '<p>' + numbers_received[i].toString() + '</p>';
$('#log').html(numbers_string);
);
);
</script>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="jumbotron">
<h1>Asynchronous Flask Communication</h1>
<p>Random numbers generated by the Flask server will appear below, asynchronously.</p>
</div>
</div>
</div>
<div class="container" id="content">
<div class="row">
<p>Asynchronous page updates will appear here:</p>
<h3>Number list:</h3>
<div id="log">
</div> <!-- /#log -->
</div>
</div>
【问题讨论】:
【参考方案1】:试试这样的方法,也许有帮助
您可以使用将运行您的函数的线程来完成此操作
yourThread = Thread()
thread_stop_event = Event()
class someClass(Thread):
def __init__(self):
self.delay = 1
super(someClass,self).__init__()
def forklift_callback(self):
while not thread_stop_event.isSet():
number = random.randint(1,101)
print(number)
socketio.emit('newnumber', 'number': number, namespace='/test')
sleep(self.delay)
def run(self):
self.forklift_callback()
@socketio.on('connect', namespace='/test')
def test_connect():
print("CALLED")
global yourThread
if(not yourThread.isAlive()):
yourThread = someClass()
yourThread.start()
或在客户端连接您的服务器后运行它
@socketio.on('connect', namespace='/test')
def test_connect():
//your function here
print("CALLED")
【讨论】:
感谢您的建议!事实证明我不得不卸载 gevent,因为它干扰了套接字。以上是关于Flask SocketIO 消息未收到的主要内容,如果未能解决你的问题,请参考以下文章
Flask + RabbitMQ + SocketIO - 转发消息
Flask-SocketIO 未使用 Gevent/Gevent-websocket
Nginx 和 Flask-socketio Websockets:活着但不是消息传递?
如何使用 ssl 启动 flask_socketio 应用程序?