Mqttnet: Cannot access server over websocket

Created on 2 Nov 2018  路  15Comments  路  Source: chkr1011/MQTTnet

Love ur work, but how i can access server over ws ?
Tried server_ip:1883.
The connection keeps refusing

All 15 comments

Added this on startup -> config services
`
//this adds a hosted mqtt server to the services
services.AddHostedMqttServer(builder => builder.WithDefaultEndpointPort(1883));

        //this adds tcp server support based on System.Net.Socket
        services.AddMqttTcpServerAdapter();

        //this adds websocket support
        services.AddMqttWebSocketServerAdapter();`

this at startup -> configure
app.UseMqttEndpoint();

this at Program
private static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseKestrel(o => { o.ListenAnyIP(1883, l => l.UseMqtt()); // mqtt pipeline o.ListenAnyIP(5000); // default http pipeline }) .UseStartup<Startup>() .Build();

This works :
`
[Route("/A/{m}")]
public async Task Test(string m)
{
var factory = new MqttFactory();
var mqttClient = factory.CreateMqttClient();

        var options = new MqttClientOptionsBuilder()
            .WithTcpServer("192.168.1.23") // Port is optional
            .Build();

        await mqttClient.ConnectAsync(options);

        var message = new MqttApplicationMessageBuilder()
            .WithTopic("Test")
            .WithPayload(m)
            .WithExactlyOnceQoS()
            .WithRetainFlag()
            .Build();

        await mqttClient.PublishAsync(message);

    }

`

this not
` public async Task Test1(string m)
{
var factory = new MqttFactory();
var client = factory.CreateMqttClient();

        // Use WebSocket connection.
        var options = new MqttClientOptionsBuilder()
            .WithWebSocketServer("192.168.1.23:1883/mqtt")
            .Build();

        await client.ConnectAsync(options);

        var message = new MqttApplicationMessageBuilder()
            .WithTopic("Test")
            .WithPayload(m)
            .WithExactlyOnceQoS()
            .WithRetainFlag()
            .Build();

        await client.PublishAsync(message);
    }`

the Error:

`An unhandled exception occurred while processing the request.
TaskCanceledException: The operation was canceled.
System.Net.Http.ConnectHelper.ConnectAsync(string host, int port, CancellationToken cancellationToken)

HttpRequestException: The operation was canceled.
System.Net.Http.ConnectHelper.ConnectAsync(string host, int port, CancellationToken cancellationToken)

WebSocketException: Unable to connect to the remote server
System.Net.WebSockets.WebSocketHandle.ConnectAsyncCore(Uri uri, CancellationToken cancellationToken, ClientWebSocketOptions options)

MqttCommunicationException: Unable to connect to the remote server
MQTTnet.Adapter.MqttChannelAdapter.WrapException(Exception exception)`

1883 is the tcp port if you want to use websocket it will be on the http port

which http port ? my application runs at http://localhost:26300/
if i do this
` var factory = new MqttFactory();
var client = factory.CreateMqttClient();

            // Use WebSocket connection.
            var options = new MqttClientOptionsBuilder()
                .WithWebSocketServer("localhost:26300")
           // or WithWebSocketServer("192.168.88.8:26300")
                .Build();

            await client.ConnectAsync(options);`

still the same error

I had the same issue.. I had to add the websocket support itself...

Within the Client's Startup.cs class, in ConfigureServices(IServicesCollection services), I had to make a call to services.AddMqttWebSocketServerAdapter();, then under Configure(IApplicationBuilder app), I had to call app.UseMqttEndpoint("/myendpoint") From there, it seems to be working as expected.

HTH

and how do you make the connection ?
Like this ?
// Use WebSocket connection. var options = new MqttClientOptionsBuilder() .WithWebSocketServer("192.168.1.27:1883/myendpoint") .Build();

because this isnt working :S

Ah, websockets... let's extract the part {web}... so you would most likely connect on :443 or :80, dependent of TLS (https) or plain-text (http) connections.

here's my connection setup:

            // OK, setup the connection with the MQTT server now.
            var mqttOptions = new ManagedMqttClientOptionsBuilder()
                .WithAutoReconnectDelay(TimeSpan.FromSeconds(5))
                .WithStorage(default(IManagedMqttClientStorage)) // TODO: need to implement.
                .WithClientOptions(new MqttClientOptionsBuilder()
                    .WithClientId("Your Client Id Here")
                    .WithCredentials("Your Secret Here")
                    .WithKeepAlivePeriod(TimeSpan.FromHours(24))
                    .WithKeepAliveSendInterval(TimeSpan.FromSeconds(5))
                    .WithCleanSession()
                    .WithWebSocketServer("192.168.1.27/myendpoint")
                    .WithTls()
                    .Build()
                )
                .WithAutoReconnectDelay(TimeSpan.FromSeconds(5))
                .Build();

Now, if you're doing aspnet, you'll most likley want to connect to the aspnet endpoint.
HTH

You made my day, week, month and year ! Thanks a lot :P

.... i just heard the Friend's theme song reading your response here... 馃槅

1883 is the tcp port if you want to use websocket it will be on the http port

What this mean?
There is no WebSocket port listening ?

@JanEggers How can I subscribe in JavaScript or Angular 7 ? please advise.

@JanEggers Thank you, Its work like a charm.
Appreciate you response.

image
i got smth like this when i run it, sorry if i ask a stupid question

@andarg I cannot see any issue there. Just some warnings...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

taroyutao picture taroyutao  路  4Comments

grobmiller picture grobmiller  路  5Comments

chkr1011 picture chkr1011  路  6Comments

StefanOssendorf picture StefanOssendorf  路  9Comments

LADSoft picture LADSoft  路  4Comments