Stomp.py how to return message from listener

前端 未结 1 506
陌清茗
陌清茗 2021-01-03 09:14

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 st

相关标签:
1条回答
  • 2021-01-03 09:41

    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)
    

    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})
    
    0 讨论(0)
提交回复
热议问题