我正在开发需要推送通知的内部应用程序,但我们无法使用外部服务。我已经开始使用 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 kiosk 应用程序/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