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.51k stars 1.07k forks source link

Mqtt Broker Compatible with Web Socket #390

Closed asimturgut closed 5 years ago

asimturgut commented 6 years ago

Hi , ı want to make 3 application

  1. mqtt broker (C# console app)

    static void Main(string[] args)
        {
            Console.WriteLine("Start Broker!");
    
            // Configure MQTT server.
            // Configure MQTT server.
            var optionsBuilder = new MqttServerOptionsBuilder()
                .WithConnectionBacklog(100)
                .WithDefaultEndpointPort(1883);
            // Start a MQTT server.
            var mqttServer = new MqttFactory().CreateMqttServer();
            mqttServer.StartAsync(optionsBuilder.Build());
            Console.WriteLine("Press any key to exit.");
            Console.ReadLine();
            mqttServer.StopAsync();
        }
  2. mqtt client (C# console app)

        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                LocationName = args[0].Trim();
            }
            else
                LocationName = "L1";
    
            Console.WriteLine("Hello World!");
    
            // Create a new MQTT client.
            var factory = new MqttFactory();
            mqttClient = factory.CreateMqttClient();
    
            //// Use WebSocket connection.
            //var options = new MqttClientOptionsBuilder()
            //     .WithClientId("Client1")
            //    .WithWebSocketServer("localhost:1884/mqtt")
            //    .Build();
            // Use TCP connection.
            var options = new MqttClientOptionsBuilder()
                .WithTcpServer("localhost", 1883) // Port is optional
                //.WithWebSocketServer("ws://localhost:8000/mqtt")
                //.WithWebSocketServer("ws://localhost:9001/mqtt")
                 //.WithWebSocketServer("ws://broker.mqttdashboard.com:8000/mqtt")
                .Build();
            mqttClient.ConnectAsync(options);
            mqttClient.Connected += MqttClient_Connected;
            mqttClient.ApplicationMessageReceived += MqttClient_ApplicationMessageReceived;
            mqttClient.Disconnected += MqttClient_Disconnected;
    
            Console.WriteLine("Press any key to exit.");
            Console.ReadLine();
        }
  3. mqtt client (web app)

var ip = "127.0.0.1";
var port = 1883;
var mqttWebClientAddress = 'ws://' + ip + ':' + port ;
var topics = ["L1", "L2", "L3", "L4"];
//var options = {};
//options.protocol = "tcp";
//var options = { "protocol": "tcp:", "slashes": true, "auth": null, "host": "127.0.0.1:1884", "port": "1884", "hostname": "127.0.0.1", "hash": null, "search": "", "query": {}, "pathname": null, "path": null, "href": "tcp://127.0.0.1:1884" }

var mqttWebClient = mqtt.connect(mqttWebClientAddress)
mqttWebClient.on('error', function (error) {
console.log(error)
});

mqttWebClient.on('connect', function () {

    var mqttWebClientId = mqttWebClient.options.clientId;
    console.log(mqttWebClientId + " connected to broker");

    for (var i = 0; i < topics.length; i++) {
        var topic = topics[i];
        mqttWebClient.subscribe(topic, { qos: 1 }, function (err, granted) {
            if (err)
                console.log(topic + " : "+ err);
            else
                console.log(topic + " : "+ granted);
        });
    }
});

mqttWebClient.on('message', function (topic, message) {
        if (message != "") {

                console.log(topic+" --> "+message);
        }
});

mqtt client work fine but mqtt web client didnt work ı could not connect mqtt broker via browser because mqtt broker not compatible with web socket connection . ı want to connect mqtt broker via browser.

can you help me about this problem ? Thank you so much Best Regards

strov commented 6 years ago

Hi

WebSockets supported (via ASP.NET Core 2.0, separate nuget) Not console app.

Peter

JanEggers commented 6 years ago

as @strov said get the mqttnet.aspnetcore nuget and put it into a console app and done.

start with a template to generate a aspnet core console app: https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-new?tabs=netcore21

then add mqtt websocket support: https://github.com/chkr1011/MQTTnet/wiki/Server#aspnet-core-integration

kevinsnijder commented 5 years ago

Hey @JanEggers, I'm using .net core but this sadly does not work the same way. Is there any way to get the MQTT server with websockets working on .net core?

I currently have this error: The type 'IMqttServerOptions' is defined in an assembly that is not referenced. You must add a reference to assembly 'MQTTnet, Version=3.0.3.0, Culture=neutral, PublicKeyToken=b69712f52770c0a7'.

EDIT: Not relevant for me anymore, the issue is still there, though

JanEggers commented 5 years ago

@kevinsnijder can you please create a new issue and post a screenshot of your dependency graph? mqttnet should be included there. maybe there is something wrong in the nuget of 3.0.3

imperugo commented 5 years ago

Just updated to the 3.0.3 and got the same error message of @kevinsnijder The only way I got is to revert to the 3.0.2.

I was just using MQTTnet.AspNetCore 3.0.3. I also tried to add MQTTnet 3.0.3 (just to be sure), but nothing changed.

SeppPenner commented 5 years ago

@JanEggers We need to do some further investigation here.

okunevdm commented 5 years ago

Just updated to the 3.0.3 and got the same error message of @kevinsnijder The only way I got is to revert to the 3.0.2.

I was just using MQTTnet.AspNetCore 3.0.3. I also tried to add MQTTnet 3.0.3 (just to be sure), but nothing changed.

i have same error after update to 3.0.3 on .Net Standard 2.0 another project on Net.Framework 4.6 work correct.

chkr1011 commented 5 years ago

Did you maybe added the extension for ASP.NET manually? So is it listed as 3.0.2 in your nuget package list. If it is you must also update it to 3.0.3. If it is not there then we might have another problem.

nesherhh commented 5 years ago

Having same problem. Here is my csproj: `

`

TaherianuBeac commented 5 years ago

I also have same problem. You must add a reference to assembly 'MQTTnet, Version=3.0.2.0, Culture=neutral, PublicKeyToken=b69712f52770c0a7' also in Version 3.0.3.

SeppPenner commented 5 years ago

I will close this issue. Please check https://github.com/chkr1011/MQTTnet/issues/677 for further reference/ discussion because these seem to be duplicates.

rafiulgits commented 4 years ago

I am using .NET Core 3.1 for MQTTnet broker service. It's works fine when a client connect with mqtt protocol from MQTT.fx or .NET client; but whenever a request come from browser using ws protocol its seems like this


My .NET broker configuration

startup.cs


program.cs


I saw this issue conversation but nothing found useful for me.

SeppPenner commented 4 years ago

@rafiulgits This is resolved now in the original question under https://github.com/chkr1011/MQTTnet/issues/857, isn't it?

rafiulgits commented 4 years ago

@SeppPenner Yes.

danielstock commented 4 years ago

@asimturgut did u find the way to fix it? i have the same problem.