MQTTnet icon indicating copy to clipboard operation
MQTTnet copied to clipboard

MQTTnet.Exceptions.MqttCommunicationTimedOutException HResult=0x80131500 Message=The operation has timed out. Source=mscorlib StackTrace: at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) in System.Runtime.CompilerServices\TaskAwaiter.cs:line 78 此异常最初是在此调用堆栈中引发的:

Open shiyuanGame opened this issue 1 year ago • 1 comments

image image

尊敬的开发者,您好,我在使用mqtt协议遇到了问题,我在发布消息端 发布在 while true 内 push message sleep 100毫秒 一分钟以内必报错这个异常bug 超时 一下是客户端订阅代码

public class MosquittoClient {

 string test_topic= "test_topic";
 string ip = "10.17.17.74";
 public string Test_topic{ get => test_topic; set => test_topic= value; }
 public MosquittoClient()
 {
     init();
 }
 private async void init()
 {

     var logger = new MqttNetEventLogger();

     var factory = new MqttFactory(logger);
     var client = factory.CreateMqttClient();
     var clientOptions = new MqttClientOptions
     {
         ChannelOptions = new MqttClientTcpOptions
         {
             Server = ip,
             Port = 1883
         }
         ,
         Timeout = TimeSpan.FromSeconds(20)
     };

     client.ApplicationMessageReceivedAsync += e =>
     {
         var payloadText = string.Empty;
         if (e.ApplicationMessage.PayloadSegment.Count > 0)
         {
             payloadText = Encoding.UTF8.GetString(
                  e.ApplicationMessage.PayloadSegment.Array,
                  e.ApplicationMessage.PayloadSegment.Offset,
                  e.ApplicationMessage.PayloadSegment.Count);
                  Console.WriteLine(payloadText); 
         }
         return CompletedTask.Instance;
     };

     client.ConnectedAsync += async e =>
     {
         Console.WriteLine("### CONNECTED WITH SERVER ###");

         await client.SubscribeAsync(Test_topic);

         Console.WriteLine("### SUBSCRIBED ###");
     };

     client.DisconnectedAsync += async e =>
     {
         Console.WriteLine("### DISCONNECTED FROM SERVER ###");
         await Task.Delay(TimeSpan.FromSeconds(5));

         try
         {
             await client.ConnectAsync(clientOptions);
         }
         catch
         {
             Console.WriteLine("### RECONNECTING FAILED ###");
         }
     };

     try
     {
         await client.ConnectAsync(clientOptions);
     }
     catch (Exception exception)
     {
         Console.WriteLine("### CONNECTING FAILED ###" + Environment.NewLine + exception);
     }
 }

}

shiyuanGame avatar Feb 04 '24 08:02 shiyuanGame

补充 版本 image

image

shiyuanGame avatar Feb 04 '24 08:02 shiyuanGame