【发布时间】:2021-07-10 04:40:05
【问题描述】:
这个脚本应该是rabbitmq学习意义的信使。我正在尝试运行带有扇出交换的消息代理。
问题是:没有广播消息。这些以负载平衡方式发送。如何像真正的扇出交换一样广播这些消息?
const amqplib = require('amqplib');
const readline = require("readline");
class Chat {
async init() {
await this.configureChannel();
await this.configureConsumer();
await this.configureCommandLine();
}
async configureChannel() {
const conn = await amqplib.connect('amqp://guest:guest@localhost:5672');
const ch = await conn.createChannel();
await ch.assertExchange("chat", "fanout", {});
const { queue } = await ch.assertQueue('messages');
await ch.bindQueue(queue, 'chat', '');
this.ch = ch;
}
async configureConsumer() {
await this.ch.consume("messages", logMessage);
function logMessage(msg) {
if (msg.content)
console.log("\n[*] Recieved message: '%s'", msg.content.toString())
}
}
async configureCommandLine() {
const commandLine = readline.createInterface({
input: process.stdin,
output: process.stdout
});
this.commandLine = commandLine;
}
async run() {
const prompt = () => {
this.commandLine.question("Message: ", async (mensagem) => {
debugger;
if (mensagem === "sair") {
return this.commandLine.close();
}
await this.ch.publish("chat", 'messages', Buffer.from(mensagem), {});
prompt();
});
}
await this.init();
console.log("\nChat\n");
prompt();
}
}
const chat = new Chat();
chat.run();
【问题讨论】:
标签: node.js rabbitmq node-modules