我有以下情况。我想使用amqp客户端实现rabbitmq的pub / sub模型。我有订阅者订阅特定ID。我想在那个id的名字上形成一个队列。因此,想要订阅该特定id的所有订阅者都应该订阅该特定队列。所以我想基于具有该id的所有订户的特定id形成唯一队列。但在我的情况下,它正在为相同的id形成不同的队列(我猜)。结果,特定id的订户能够以循环方式接收所有消息,尽管所有订户都接收所有消息。以下是我正在使用的代码。
var express = require('express');
var amqp = require('amqp');
var app = express();
var httpServer = require('http').createServer();
var socketio = require('socket.io');
var io = socketio.listen(httpServer);
httpServer.listen(8000);
console.log("server started at port no 8000");
var rabbitConnection = amqp.createConnection({host:"localhost", port:"5672"});
var chatExchange;
rabbitConnection.on("ready", function()
{
console.log("RabbitConnection got ready");
chatExchange = rabbitConnection.exchange("chatExchange", {"type":"topic"});
//console.log("value of chatExchange is :",chatExchange);
});
io.sockets.on("connection", function(socket)
{
console.log("socket connected");
socket.on("metaData", function(data)
{
if(data.type=="publisher")
{
console.log("publisher connected");
socket.type="publisher";
}
else if(data.type=="subscriber")
{
console.log("subscriber connected");
socket.type="subscriber";
rabbitConnection.queue(""+data.channelName, function(queue) //MAIN PROBLEM LIES IN ABOVE LINE I GUESS. IT WILL FORM QUEUE FOR EVERY SOCKET CONNECTION. BUT IT SHOULD NOT FORM QUEUE IF IT ALREADY EXISTS.
{
console.log("queue formed");
console.log("queue is ", queue);
queue.bind("chatExchange",""+data.channelName);
queue.subscribe(function(message)
{
console.log("message is "+message.data.toString());
console.log("message emitted");
socket.emit("message",{"text":message.data.toString()});
});
});
}
});
socket.on("disconnect",function()
{
console.log("socket disconnected");
});
socket.on("message", function(data)
{
chatExchange.publish(""+data.channelName, data.text);
console.log("message came");
});
})
我见过node-amqp模块的文档。但我没有找到任何选择来破坏队列的存在。那里有一些“持久性”选择。但我不认为它会起作用。请帮忙.... 提前谢谢..