Stomp.py如何从侦听器返回消息 [英] Stomp.py how to return message from listener

查看:371
本文介绍了Stomp.py如何从侦听器返回消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读过此主题: Stomp.py从收听者返回消息

I've already read this topic: Stomp.py return message from listener

但是我仍然不知道这是如何工作的,为什么没有办法从踩踏对象或监听器直接获取消息?

But i still don't get how this works, and why there's no way to retrieve a message from the stomp object, or the listener directly?

如果我可以通过发送方式发送消息,并且如果我可以使用on_message侦听器方法接收消息,为什么我不能将该消息返回到我的原始功能,所以我可以把它退回到前端?

If i can send a message via the send method, and If i can receive a message with an on_message listener method, why can't I return that message to my original function, so I could return it to the frontend?

所以如果我有:

class MyListener(object):
    def on_error(self, headers, message):
        print '>>> ' + message
    def on_message(self, headers, message):
        print '>>> ' + message

如何从on_message方法返回邮件?

how could I return a message from the on_message method?

或者我可以在conn.subscribe(...)之后以某种方式做...

or could I do it somehow after the conn.subscribe(...) ??

推荐答案

Ok, I found a way myself. All you have to do, is a slight change of the listener class:

class MyListener(object):
    msg_list = []

    def __init__(self):
        self.msg_list = []

    def on_error(self, headers, message):
        self.msg_list.append('(ERROR) ' + message)

    def on_message(self, headers, message):
        self.msg_list.append(message)

在代码中,你使用stomp.py:

And in the code, where u use stomp.py:

conn = stomp.Connection()
lst = MyListener()
conn.set_listener('', lst)
conn.start()
conn.connect()
conn.subscribe(destination='/queue/test', id=1, ack='auto')
time.sleep(2)
messages = lst.msg_list
conn.disconnect()
return render(request, 'template.html', {'messages': messages})

这篇关于Stomp.py如何从侦听器返回消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆