抛出了类型为'uPLibrary.Networking.M2Mqtt.Exceptions.MqttClientException'的异常

5

我正在连接到mqtt,但是收到了一个没有帮助的异常。

代码

string smsTopic = ConfigurationManager.AppSettings["MQTT_SMS_Topic"];
string emailTopic = ConfigurationManager.AppSettings["MQTT_Email_Topic"];
string pushTopic = ConfigurationManager.AppSettings["MQTT_PUSH_Topic"];
string socialTopic = ConfigurationManager.AppSettings["MQTT_SOCIAL_Topic"];

client = new MqttClient("somehostname");
string clientId = Guid.NewGuid().ToString();
client.Connect(clientId);
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
client.Subscribe(new string[] { smsTopic, emailTopic, pushTopic, socialTopic }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });

异常消息

抛出了类型为'uPLibrary.Networking.M2Mqtt.Exceptions.MqttClientException'的异常

异常的堆栈跟踪

at uPLibrary.Networking.M2Mqtt.Messages.MqttMsgSubscribe.GetBytes(Byte protocolVersion) in c:\Users\ppatierno\Source\Repos\m2mqtt\M2Mqtt\Messages\MqttMsgSubscribe.cs:line 187
at uPLibrary.Networking.M2Mqtt.MqttClient.Send(MqttMsgBase msg) in c:\Users\ppatierno\Source\Repos\m2mqtt\M2Mqtt\MqttClient.cs:line 1028
at uPLibrary.Networking.M2Mqtt.MqttClient.ProcessInflightThread() in c:\Users\ppatierno\Source\Repos\m2mqtt\M2Mqtt\MqttClient.cs:line 1954
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

我在Github上发现了一个问题bug,但目前还没有解决方案。

异常信息完全没有帮助,并且其中也没有内部异常信息。

3个回答

8
我能够通过更改以下语句来解决这个奇怪的异常。
client.Subscribe(new string[] { smsTopic, emailTopic, pushTopic, socialTopic }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });

为了

client.Subscribe(new string[] { smsTopic }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
client.Subscribe(new string[] { emailTopic }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
client.Subscribe(new string[] { pushTopic }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
client.Subscribe(new string[] { socialTopic }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });

看起来当同时指定多个主题时,mqtt存在缺陷。


5

更好的写法:

client.Subscribe(new string[] 
    { smsTopic, emailTopic, pushTopic, socialTopic }, 
    new byte[] { 
         MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, 
         MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE,
         MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE,
         MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE 
    }
);

2
我关注这个博客:https://www.hivemq.com/blog/mqtt-client-library-encyclopedia-m2mqtt 如果你阅读订阅部分,client.Subscribe( )需要两个数组作为输入参数:前者是你想要订阅的topics列表,后者是每个主题对应的QoS级别列表(每个主题一个)
这意味着你需要为每个主题传递服务质量(QoS)级别。
这将解决你的问题。
client.Subscribe(new string[] { smsTopic }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
client.Subscribe(new string[] { emailTopic }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
client.Subscribe(new string[] { pushTopic }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
client.Subscribe(new string[] { socialTopic }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });

你的答案是从两年前写的另一个答案复制过来的,重复回答没有任何意义。 - Hakan Fıstık

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接