Closed VallatMa closed 4 years ago
Here is the complete error:
MQTTnet.Exceptions.MqttCommunicationException: Unable to connect to the remote server ---> System.Net.WebSockets.WebSocketException: Unable to connect to the remote server
at System.Net.WebSockets.WebSocketHandle+
1) Remove -> app.UseHttpsRedirection();
2) Add endpoint in Configure
app.UseEndpoints(endpoints =>
{
endpoints.MapMqtt("/mqtt"); // <<------- this line
endpoints.MapRazorPages();
endpoints.MapControllers();
endpoints.MapFallbackToFile("index.html");
});
webBuilder
.UseKestrel(o =>
{
o.ListenAnyIP(mqttPipeline, l => l.UseMqtt());
o.ListenAnyIP(httpPipeline);
//If app.UseHttpsRedirection(); is applied in startup.cs
o.ListenAnyIP(httpsPipeline, l => l.UseHttps());
})
.UseStartup<Startup>();
In startup.cs
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
//Setup mqtt endpoints for websocket (localhost:{port}/mqtt}
endpoints.MapMqtt("/mqtt");
});
Client connection
// WebSocket
var options = new MqttClientOptionsBuilder()
.WithClientId(id)
//Protocol does not need to be included in parameter
.WithWebSocketServer("127.0.0.1:5001/mqtt")
//as app.UseHttpsRedirection() is applied in Server, WithTls must be apply
.WithTls()
.WithKeepAlivePeriod(TimeSpan.FromHours(24))
.WithKeepAliveSendInterval(TimeSpan.FromSeconds(5))
.WithCleanSession()
.Build();
More information Web socket client AspNetCore3.1 Server
@JimmyPun610's solution should work, as far as I can see from the examples :)
Thanks for all your answers :) Because of some time pressure, I made it work by changing from WebSocket to TCP.
So here my working setup:
Startup.cs ConfigureServices
services.AddSingleton<MqttService>();
services.AddHostedMqttServerWithServices(options => {
var s = options.ServiceProvider.GetRequiredService<MqttService>();
s.ConfigureMqttServerOptions(options);
});
services.AddConnections();
services.AddMqttConnectionHandler();
Startup.cs Configure
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
endpoints.MapMqtt("/mqtt");
});
app.UseMqttServer(server => app.ApplicationServices.GetRequiredService<MqttService>().ConfigureMqttServer(server));
Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => {
webBuilder.ConfigureKestrel(serverOptions => {
serverOptions.ListenAnyIP(1883, l => l.UseMqtt());
serverOptions.ListenAnyIP(5000, l => l.UseHttps());
}).UseStartup<Startup>();
});
Describe your question
I'm trying to do a server REST and MQTT with ASP.net Core MVC for working with Unity3D. The REST server is working fine. I'm facing that issue when trying to connect to the server with WebSocket: QTTnet.Exceptions.MqttCommunicationException: Unable to connect to the remote server ---> System.Net.WebSockets.WebSocketException: Unable to connect to the remote server
I tried for one week to make it work but I'm running out of ideas. I would be grateful if you guys can tell me what did I missed.
Versions: Microsoft.AspDotNetCore.App 3.1.2 Microsoft.NETCore.App 3.1.0 Microsoft.AspNetCore.Mvc.NewtonsoftJson 3.1.3 MQTTnet 3.0.11 MQTTnet.AspNetCore 3.0.11
The client connection part:
The server Programm class:
The server Configure Service:
The server Configure
The server MQTT service witout all the implemented interfaces
Thanks in advance !
edit: changed the versions to 3.0.11