连接到多个代理时如何知道哪个代理发布了消息

时间:2019-05-21 13:10:54

标签: client mqtt

我有一些JavaScript代码通过使用2个不同的客户端对象来连接/订阅两个代理。我的问题是如何判断消息在onMessageArrived中从何而来。我收到消息,但我无法确定是哪个经纪人。有办法知道吗?

1 个答案:

答案 0 :(得分:1)

你不能

解决方法是拥有3个onMessageArrived函数:

client1.onMessageArrived = onMessageArrivedOne;
client2.onMessageArrived = onMessageArrivedTwo;

function onMessageArrivedOne(message) {
  message.broker = 1;
  onMessageArrived(message)
}

function onMessageArrivedTwo(message) {
  message.broker = 2;
  onMessageArrived(message)
}

function onMessageArrived(message) {
  //do what you need
}