我正在开发需要推送通知的内部应用程序,但是我们不能使用外部服务。我已经开始使用RabbitMQ,并且在.NET Core中可以完美地工作。当试图用javascript实现同一件事时,我没有得到相同的结果。
我用C#开发了测试客户。我用javascript开发了一个客户端。我可以建立成功的连接,但是数据没有到达。
在C#中,我正在使用:
string e = Console.ReadLine();
Console.WriteLine("Enter a message (blank for test msg)");
string message = Console.ReadLine();
var factory = new ConnectionFactory() { HostName = "10.222.2.160" };
factory.UserName = "Test";
factory.Password = "TestPassword";
factory.VirtualHost = "/";
using (var connection = factory.CreateConnection("TestChannel"))
using (var channel = connection.CreateModel())
{
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: e,
routingKey: "",
basicProperties: null,
body: body);
Console.WriteLine(" [x] Sent {0}", message);
}
用Javascript:
var wsbroker = "10.222.2.160"; // mqtt websocket enabled broker
var wsport = 15675; // port for above
var client = new Paho.MQTT.Client(wsbroker, wsport, "/ws/",
"test");
client.onConnectionLost = function (responseObject) {
console.log("CONNECTION LOST - " + responseObject.errorMessage);
};
client.onMessageArrived = function (message) {
console.log("RECEIVE ON " + message.destinationName + " PAYLOAD " + message.payloadString);
};
var options = {
userName: "Test",
password: "TestPassword",
timeout: 3,
keepAliveInterval: 30,
onSuccess: function () {
console.log("CONNECTION SUCCESS");
client.subscribe('test', { qos: 1 });
},
onFailure: function (message) {
console.log("CONNECTION FAILURE - " + message.errorMessage);
}
};
if (location.protocol == "https:") {
options.useSSL = true;
}
console.log("CONNECT TO " + wsbroker + ":" + wsport);
client.connect(options);
我需要能够从javascript(非节点,chrome信息亭应用程序/ chrome扩展名)连接到Rabbitmq。但是,我不确定我是否“了解” RabbitMQ。为我指出正确的方向将帮助一个女孩。谢谢!
最佳答案
您在此处发布时没有路由键:
channel.BasicPublish(exchange: e,
routingKey: "",
basicProperties: null,
body: body);
确保
test
队列存在,然后在发布者中将routingKey
更改为test
,并使用名为amq.direct
的交换。您应该阅读此处提供的RabbitMQ简介,以熟悉交换,队列,路由键和绑定的交互方式:
https://www.cloudamqp.com/blog/2015-05-18-part1-rabbitmq-for-beginners-what-is-rabbitmq.html
注意:RabbitMQ团队监视
rabbitmq-users
mailing list,并且有时仅在StackOverflow上回答问题。关于javascript - 在Chrome信息亭/扩展程序/应用和网络浏览器中实现RabbitMQ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57295146/