def stream_topic(topic_name):
"""
GET /api/<version>/topic_stream/<topic_name>
Stream a topic over HTTP by keeping the http connection alive.
"""
topic_name = "/" + topic_name
try:
msg_class, real_topic, _ = rostopic.get_topic_class(topic_name)
except rostopic.ROSTopicIOException as e:
raise e
if not real_topic:
return error("Topic does not exist", 404)
queue = Queue(5)
def callback(dataIn, queue=queue):
data = getattr(dataIn, "data", None)
if data is None:
data = {"header": getattr(dataIn, "header"), "status": getattr(dataIn, "status")}
queue.put(data)
sub = rospy.Subscriber(real_topic, msg_class, callback)
def gen(queue=queue):
while True:
x = queue.get()
yield str(x) + "\n"
return Response(gen(), mimetype='text/plain')
评论列表
文章目录