Exemple #1
0
        private static async Task ExecuteTestAsync(string name, IMqttClientOptions options)
        {
            try
            {
                Write("Testing '" + name + "'... ", ConsoleColor.Gray);
                var factory = new MqttFactory();
                var client  = factory.CreateMqttClient();
                var topic   = Guid.NewGuid().ToString();

                MqttMessage receivedMessage = null;
                client.ApplicationMessageReceived += (s, e) => receivedMessage = e.ApplicationMessage;

                await client.ConnectAsync(options);

                await client.SubscribeAsync(topic, MqttQualityOfServiceLevel.AtLeastOnce);

                await client.PublishAsync(topic, "Hello_World", MqttQualityOfServiceLevel.AtLeastOnce);

                SpinWait.SpinUntil(() => receivedMessage != null, 5000);

                if (receivedMessage?.Topic != topic || receivedMessage?.ConvertPayloadToString() != "Hello_World")
                {
                    throw new Exception("Message invalid.");
                }

                await client.UnsubscribeAsync("test");

                await client.DisconnectAsync();

                Write("[OK]\n", ConsoleColor.Green);
            }
            catch (Exception e)
            {
                Write("[FAILED] " + e.Message + "\n", ConsoleColor.Red);
            }
        }
Exemple #2
0
        private static void MqttClient_MessageReceived(object sender, MqttMessage e)
        {
            var strBuilder = new StringBuilder();

            strBuilder.Append("------------------------------------------------------");
            strBuilder.Append(Environment.NewLine);
            strBuilder.Append($"{DateTime.Now.ToLongDateString()} {DateTime.Now.ToLongTimeString()} Mqttnet Application Message Received");
            strBuilder.Append(Environment.NewLine);
            strBuilder.Append($"{DateTime.Now.ToLongDateString()} {DateTime.Now.ToLongTimeString()} Topic:{e.Topic}");
            strBuilder.Append(Environment.NewLine);
            strBuilder.Append($"{DateTime.Now.ToLongDateString()} {DateTime.Now.ToLongTimeString()} Payload:{e.ConvertPayloadToString()}");
            strBuilder.Append(Environment.NewLine);
            strBuilder.Append($"{DateTime.Now.ToLongDateString()} {DateTime.Now.ToLongTimeString()} Retain:{e.Retain}");
            strBuilder.Append(Environment.NewLine);
            strBuilder.Append($"{DateTime.Now.ToLongDateString()} {DateTime.Now.ToLongTimeString()} QualityOfServiceLevel:{e.Qos.ToString()}");
            strBuilder.Append(Environment.NewLine);
            strBuilder.Append("------------------------------------------------------");
            strBuilder.Append(Environment.NewLine);

            System.Console.WriteLine(strBuilder);
        }
        public static object ConvertPayloadToObject(this MqttMessage message)
        {
            var str = message.ConvertPayloadToString();

            return(JsonConvert.DeserializeObject(str));
        }