我有一个网页,应该使用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
没有运行。有什么办法可以解决这个问题吗?
谢谢!
1条答案
按热度按时间2wnc66cl1#
最终使用stomp.py而不是pika: