在Electron应用中使用 amqplib 库来连接到RabbitMQ服务器并进行通信
const amqp = require('amqplib');
// 连接信息
const rabbitMQConfig = {
hostname: 'your-rabbitmq-hostname',
port: 5672,
username: 'your-username',
password: 'your-password',
};
// 连接到RabbitMQ服务器
amqp.connect(`amqp://${rabbitMQConfig.hostname}:${rabbitMQConfig.port}`, {
username: rabbitMQConfig.username,
password: rabbitMQConfig.password,
}).then((connection) => {
// 创建通道
return connection.createChannel();
}).then((channel) => {
// 声明队列
const queueName = 'your-queue-name';
return channel.assertQueue(queueName).then(() => {
// 发送消息
const message = 'Hello, RabbitMQ!';
channel.sendToQueue(queueName, Buffer.from(message));
console.log(`[x] Sent '${message}'`);
});
}).catch((error) => {
console.error('Error:', error);
});