请
不要犹豫,提供任何可能有助于回答您关于 SO 的问题的环境详细信息:例如你正在使用的包,你如何初始化你正在使用的变量,等等。
回答
在amqplib documentation(我猜这是您正在使用的包)中,有一个specific type of channel 使用确认 表示RabbitMQ 服务器已成功接收到消息:
import * as amqp from "amqplib";
// Initialise `AMQP_OPTIONS` and `message` here, e.g.:
/* const AMQP_OPTIONS = {
frameMax: 0,
heartbeat: 0,
hostname: "localhost",
locale: "en_GB",
password: "guest",
port: 5672,
protocol: "amqp",
username: "guest",
vhost: "/",
};
const message = "Hello, world!"; */
const connection = await amqp.connect(AMQP_OPTIONS);
const confirmChannel = await connection.createConfirmChannel();
await confirmChannel.assertQueue(config.amqp.queue);
confirmChannel.sendToQueue(
config.amqp.queue, // Perhaps you wanted 'config.asteriskAmi.queue' here ?
Buffer.from(message),
{},
(err, ok) => {
if (err !== null) {
console.error(err);
// Error handling
}
else {
console.info("Message successfully acked!");
// Success handling
}
}
);
注意:您的问题也可能是您的消费者订阅了config.amqp.queue,而您发布到config.asteriskAmi.queue。
希望对您有所帮助。如果您需要有关confirmChannel.sendToQueue 的更多详细信息,请查看documentation。