MQTTnet icon indicating copy to clipboard operation
MQTTnet copied to clipboard

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

Open wolfcome110 opened this issue 1 year ago • 1 comments
trafficstars

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'
  • Client ` var host = ‘127.0.0.1; var port = 10086; var path = ’‘; var clientID = ’123‘; //clientID var user = ’admin; var password = '123456'; var url = host+':'+port+path;

    var options = { clientID:clientID, username:user, password:password }; client = mqtt.connect(url,options); `

`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;
}
`

wolfcome110 avatar Jun 13 '24 07:06 wolfcome110

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

mimimiaomiao1 avatar Jun 24 '24 02:06 mimimiaomiao1