MqttCommunicationTimedOutException when connecting
- Dominant language
- C#
- Stars
- 5.1k
- Forks
- 1.2k
- PR merge metrics
- No merged PRs in 30d
Description
### Describe the bug
Attempting to connect to a Mqttnet server with the default client and receiving a timeout
### Which project is your bug related to?
- Client
- Server
### To Reproduce
.Net 5 web app with the following code
server
```
ConfigureServices
services.AddHostedMqttServer(mqttServer =>
mqttServer.WithoutDefaultEndpoint()
.WithDefaultEndpointPort(5001).WithEncryptionCertificate(
X509Certificate2.CreateFromPemFile(
"server.crt",
"server.key")))
.AddMqttConnectionHandler()
.AddConnections(options => options.DisconnectTimeout = TimeSpan.FromMinutes(1));
Configure
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapConnectionHandler(
"/mqtt",
httpConnectionDispatcherOptions => httpConnectionDispatcherOptions.WebSockets.SubProtocolSelector =
protocolList =>
protocolList.FirstOrDefault() ?? string.Empty);
});
app.UseMqttServer(server =>
{
server.UseClientConnectedHandler(args => Console.WriteLine($"Client connected with id {args.ClientId}"));
server.UseClientConnectedHandler(args => Console.WriteLine($"Client disconnected with id {args.ClientId}"));
server.UseApplicationMessageReceivedHandler(args =>
Console.WriteLine(
$"Message received on Mqtt broker with {args.ClientId} + {args.ApplicationMessage.Topic} + {args.ApplicationMessage.Payload}"));
});
```
client
```
public static void StartClient(string clientId)
{
Task.Delay(TimeSpan.FromSeconds(10)).ContinueWith((async task =>
{
Client.Instance.UseConnectedHandler(e =>
{
Console.WriteLine("### CONNECTED WITH SERVER ###");
});
Client.Instance.UseDisconnectedHandler(e =>
{
Console.WriteLine($"Disconnected from broker + {e.Reason} + {e.Exception} + {e.Exception?.InnerException}+ {e.AuthenticateResult} + {e.ReasonCode}");
});
Client.Instance.UseApplicationMessageReceivedHandler(e =>
{
var payload = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
Console.WriteLine($"### {clientId} RECEIVED APPLICATION MESSAGE ###");
Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
Console.WriteLine($"+ Payload = {payload}");
Console.WriteLine();
});
//if (Client.Instance.IsConnected)
// Client.Instance.DisconnectAsync();
Console.WriteLine("Starting MQTT Client");
var connectResult = await Client.Instance.ConnectAsync(
new MqttClientOptionsBuilder().WithClientId(clientId)
.WithTcpServer("localhost", 5001)
.WithTls(options =>
{
options.Certificates = new[]
{X509Certificate.CreateFromCertFile("ca.crt")};
options.AllowUntrustedCertificates = true;
})
.WithKeepAlivePeriod(TimeSpan.FromSeconds(60))
.Build());
Console.WriteLine("Connected");
}));
}
```
### Expected behavior
Unsure why it times out. The same certificates work with a mosquittoo server/client sample
Exception of type 'MQTTnet.Exceptions.MqttCommunicationTimedOutException' was thrown.
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at MQTTnet.PacketDispatcher.MqttPacketAwaiter`1.d__6.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at MQTTnet.Client.MqttClient.d__50`1.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at MQTTnet.Client.MqttClient.d__44.MoveNext()
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reproducing the timeout from the server endpoint configuration and the client's ConnectAsync call using WithTcpServer and WithTls. Trace the connection and authentication path to determine why the MQTT client receives no response, then verify the cause with a focused regression test or reproducible configuration.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- networking
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100