Ejemplo n.º 1
0
        // This code is used at the Wiki on GitHub!
        // ReSharper disable once UnusedMember.Local
        private static async void WikiCode()
        {
            {
                var client = new MqttFactory().CreateMqttClient();

                var options = new MqttClientOptionsBuilder()
                              .WithClientId("Client1")
                              .WithTcpServer("broker.hivemq.com")
                              .WithCredentials("bud", "%spencer%")
                              .WithTls()
                              .Build();

                await client.ConnectAsync(options);

                var message = new MqttApplicationMessageBuilder()
                              .WithTopic("MyTopic")
                              .WithPayload("Hello World")
                              .WithExactlyOnceQoS()
                              .WithRetainFlag()
                              .Build();

                await client.PublishAsync(message);
            }

            {
                var factory = new MqttFactory();
                factory.GetLoggerFactory().AddConsole();

                var client = factory.CreateMqttClient();
            }
        }
Ejemplo n.º 2
0
        public static async Task RunAsync()
        {
            try
            {
                var options = new MqttClientOptions
                {
                    ClientId       = "XYZ",
                    CleanSession   = true,
                    ChannelOptions = new MqttClientTcpOptions
                    {
                        Server = "localhost"
                    },
                    ////ChannelOptions = new MqttClientWebSocketOptions
                    ////{
                    ////    Uri = "localhost"
                    ////}
                };

                var factory = new MqttFactory();
                factory.GetLoggerFactory().AddConsole();

                var client = factory.CreateMqttClient();

                client.ApplicationMessageReceived += (s, e) =>
                {
                    Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
                    Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
                    Console.WriteLine($"+ Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
                    Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
                    Console.WriteLine($"+ Retain = {e.ApplicationMessage.Retain}");
                    Console.WriteLine();
                };

                client.Connected += async(s, e) =>
                {
                    Console.WriteLine("### CONNECTED WITH SERVER ###");

                    await client.SubscribeAsync(new TopicFilterBuilder().WithTopic("#").Build());

                    Console.WriteLine("### SUBSCRIBED ###");
                };

                client.Disconnected += async(s, e) =>
                {
                    Console.WriteLine("### DISCONNECTED FROM SERVER ###");
                    await Task.Delay(TimeSpan.FromSeconds(5));

                    try
                    {
                        await client.ConnectAsync(options);
                    }
                    catch
                    {
                        Console.WriteLine("### RECONNECTING FAILED ###");
                    }
                };

                try
                {
                    await client.ConnectAsync(options);
                }
                catch (Exception exception)
                {
                    Console.WriteLine("### CONNECTING FAILED ###" + Environment.NewLine + exception);
                }

                Console.WriteLine("### WAITING FOR APPLICATION MESSAGES ###");

                while (true)
                {
                    Console.ReadLine();

                    var applicationMessage = new MqttApplicationMessageBuilder()
                                             .WithTopic("A/B/C")
                                             .WithPayload("Hello World")
                                             .WithAtLeastOnceQoS()
                                             .Build();

                    await client.PublishAsync(applicationMessage);
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }