dotnet / MQTTnet

MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker). The implementation is based on the documentation from http://mqtt.org/.
MIT License
4.35k stars 1.05k forks source link

WebSocket connection to 'ws://127.0.0.1:10086/' failed: #2021

Open wolfcome110 opened 1 month ago

wolfcome110 commented 1 month ago

How to configure the mqtt server?

Steps to reproduce the behavior:

  1. Using this version of MQTTnet '4.3.6.1152'.
  2. Run this code '....'.
  3. See error:'mqtt.min.js:5 WebSocket connection to 'ws://127.0.0.1:10086/' failed: ',' Connection error:Error: WebSocket error'
`builder.Services.AddHostedService<MqttHostService>();`

MqttHostService.cs
`
public Task StartAsync(CancellationToken cancellationToken)
{
    MqttServerOptionsBuilder optionsBuilder = new MqttServerOptionsBuilder();
    optionsBuilder.WithDefaultEndpoint();
    optionsBuilder.WithDefaultEndpointPort(10086); 

    optionsBuilder.WithConnectionBacklog(1000); 
    //optionsBuilder.AddWebSocketEndpoint("0.0.0.0", 9001);
    MqttServerOptions options = optionsBuilder.Build();

    MqttService._mqttServer = new MqttFactory().CreateMqttServer(options);

    MqttService._mqttServer.ClientConnectedAsync += _mqttServer_ClientConnectedAsync; 
    MqttService._mqttServer.ClientDisconnectedAsync += _mqttServer_ClientDisconnectedAsync; 
    MqttService._mqttServer.ApplicationMessageNotConsumedAsync += _mqttServer_ApplicationMessageNotConsumedAsync; 

    MqttService._mqttServer.ClientSubscribedTopicAsync += _mqttServer_ClientSubscribedTopicAsync;
    MqttService._mqttServer.ClientUnsubscribedTopicAsync += _mqttServer_ClientUnsubscribedTopicAsync; 
    MqttService._mqttServer.StartedAsync += _mqttServer_StartedAsync; 
    MqttService._mqttServer.StoppedAsync += _mqttServer_StoppedAsync; 
    MqttService._mqttServer.InterceptingPublishAsync += _mqttServer_InterceptingPublishAsync; 
    MqttService._mqttServer.ValidatingConnectionAsync += _mqttServer_ValidatingConnectionAsync; 

    MqttService._mqttServer.StartAsync();
    return Task.CompletedTask;
}
`
mimimiaomiao1 commented 1 month ago

我也连不上。。我的前端是vue,