|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using System;
- using System.Text;
- using System.Threading.Tasks;
- using MQTTnet.Client;
- using MQTTnet.Client.Connecting;
- using MQTTnet.Client.Disconnecting;
- using MQTTnet.Client.Options;
- using MQTTnet.Client.Receiving;
- using MQTTnet.Protocol;
-
- namespace MQTTnet.TestApp.NetCore
- {
- public static class ClientTest
- {
- public static async Task RunAsync()
- {
- try
- {
- MqttNetConsoleLogger.ForwardToConsole();
-
- var factory = new MqttFactory();
- var client = factory.CreateMqttClient();
- var clientOptions = new MqttClientOptions
- {
- ChannelOptions = new MqttClientTcpOptions
- {
- Server = "127.0.0.1"
- }
- };
-
- client.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(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.ConnectedHandler = new MqttClientConnectedHandlerDelegate(async e =>
- {
- Console.WriteLine("### CONNECTED WITH SERVER ###");
-
- await client.SubscribeAsync(new TopicFilterBuilder().WithTopic("#").Build());
-
- Console.WriteLine("### SUBSCRIBED ###");
- });
-
- client.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(async e =>
- {
- Console.WriteLine("### DISCONNECTED FROM SERVER ###");
- await Task.Delay(TimeSpan.FromSeconds(5));
-
- try
- {
- await client.ConnectAsync(clientOptions);
- }
- catch
- {
- Console.WriteLine("### RECONNECTING FAILED ###");
- }
- });
-
- try
- {
- await client.ConnectAsync(clientOptions);
- }
- catch (Exception exception)
- {
- Console.WriteLine("### CONNECTING FAILED ###" + Environment.NewLine + exception);
- }
-
- Console.WriteLine("### WAITING FOR APPLICATION MESSAGES ###");
-
- while (true)
- {
- Console.ReadLine();
-
- await client.SubscribeAsync(new TopicFilter { Topic = "test", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce });
-
- var applicationMessage = new MqttApplicationMessageBuilder()
- .WithTopic("A/B/C")
- .WithPayload("Hello World")
- .WithAtLeastOnceQoS()
- .Build();
-
- await client.PublishAsync(applicationMessage);
- }
- }
- catch (Exception exception)
- {
- Console.WriteLine(exception);
- }
- }
- }
- }
|