stomp.js无法接收pika发送的消息(RabbitMQ版本:第3.11.7节)

zwghvu4y  于 2023-01-26  发布在  RabbitMQ
关注(0)|答案(1)|浏览(185)

我有一个网页,应该使用STOMP从RabbitMQ接收消息:

<body>
    <script src="stomp.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/sockjs-client@1.1/dist/sockjs.min.js"></script>

    <script>
        var client = Stomp.client('ws://localhost:15674/ws');

        client.debug = null;
        var sub = function(d) {
            // print_first(d.body);
            console.log("got the message! ", d.body)
        }
        var on_connect = function(x) {
            id = client.subscribe("/topic/test", sub);
            console.log("connected")
        };
        var on_error =  function() {
          console.log('error');
        };
        
        client.connect('guest', 'guest', on_connect, on_error, '/');
    </script>
</body>

当我运行这段代码时,它在控制台中显示connected(到目前为止还不错)
我还有一个python后端,它应该向队列(send.py)发送消息:

import pika

connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare("/topic/test")

properties = pika.BasicProperties(
        content_type='application/json',
        content_encoding='utf-8',
        delivery_mode=2,
    )

channel.basic_publish(exchange='',
                      routing_key='/topic/test',
                      body='Hello World!',
                      properties=properties)

消息被发送(我用py -m send运行脚本;消息出现在RabbitMQ管理中):

但是sub中的console.log没有运行。有什么办法可以解决这个问题吗?
谢谢!

2wnc66cl

2wnc66cl1#

最终使用stomp.py而不是pika:

import stomp
PORT = 61613
LOCALHOST = '0.0.0.0'
conn = stomp.Connection11([(LOCALHOST, PORT)])  
conn.connect('guest','guest')
conn.send(body="start",destination='/queue/test')
conn.send(body="end",destination='/queue/test')
conn.disconnect()

相关问题