|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using System;
- using System.Threading.Tasks;
- using System.IO;
- using Newtonsoft.Json;
- using System.Collections.Generic;
- using MQTTnet.Client.Options;
- using MQTTnet.Client.Receiving;
- using MQTTnet.Extensions.ManagedClient;
- using MQTTnet.Protocol;
-
- namespace MQTTnet.TestApp.NetCore
- {
- public static class ManagedClientTest
- {
- public static async Task RunAsync()
- {
- var ms = new ClientRetainedMessageHandler();
-
- var options = new ManagedMqttClientOptions
- {
- ClientOptions = new MqttClientOptions
- {
- ClientId = "MQTTnetManagedClientTest",
- Credentials = new RandomPassword(),
- ChannelOptions = new MqttClientTcpOptions
- {
- Server = "broker.hivemq.com"
- }
- },
-
- AutoReconnectDelay = TimeSpan.FromSeconds(1),
- Storage = ms
- };
-
- try
- {
- var managedClient = new MqttFactory().CreateManagedMqttClient();
- managedClient.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(e =>
- {
- Console.WriteLine(">> RECEIVED: " + e.ApplicationMessage.Topic);
- });
-
- await managedClient.PublishAsync(builder => builder.WithTopic("Step").WithPayload("1"));
- await managedClient.PublishAsync(builder => builder.WithTopic("Step").WithPayload("2").WithAtLeastOnceQoS());
-
- await managedClient.StartAsync(options);
-
- await managedClient.SubscribeAsync(new TopicFilter { Topic = "xyz", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce });
- await managedClient.SubscribeAsync(new TopicFilter { Topic = "abc", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce });
-
- await managedClient.PublishAsync(builder => builder.WithTopic("Step").WithPayload("3"));
-
- Console.WriteLine("Managed client started.");
- Console.ReadLine();
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- }
- }
-
-
- public class RandomPassword : IMqttClientCredentials
- {
- public byte[] Password => Guid.NewGuid().ToByteArray();
-
- public string Username => "the_static_user";
- }
-
- public class ClientRetainedMessageHandler : IManagedMqttClientStorage
- {
- private const string Filename = @"RetainedMessages.json";
-
- public Task SaveQueuedMessagesAsync(IList<ManagedMqttApplicationMessage> messages)
- {
- File.WriteAllText(Filename, JsonConvert.SerializeObject(messages));
- return Task.FromResult(0);
- }
-
- public Task<IList<ManagedMqttApplicationMessage>> LoadQueuedMessagesAsync()
- {
- IList<ManagedMqttApplicationMessage> retainedMessages;
- if (File.Exists(Filename))
- {
- var json = File.ReadAllText(Filename);
- retainedMessages = JsonConvert.DeserializeObject<List<ManagedMqttApplicationMessage>>(json);
- }
- else
- {
- retainedMessages = new List<ManagedMqttApplicationMessage>();
- }
-
- return Task.FromResult(retainedMessages);
- }
- }
- }
- }
|