You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestServerExtensions.cs 1.2 KiB

преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
преди 6 години
123456789101112131415161718192021222324252627282930313233343536373839
  1. using MQTTnet.Client;
  2. using MQTTnet.Server;
  3. using System;
  4. using System.Threading.Tasks;
  5. namespace MQTTnet.Core.Tests
  6. {
  7. public static class TestServerExtensions
  8. {
  9. /// <summary>
  10. /// publishes a message with a client and waits in the server until a message with the same topic is received
  11. /// </summary>
  12. /// <returns></returns>
  13. public static async Task PublishAndWaitForAsync(this IMqttClient client, IMqttServer server, MqttApplicationMessage message)
  14. {
  15. var tcs = new TaskCompletionSource<object>();
  16. void Handler(object sender, MqttApplicationMessageReceivedEventArgs args)
  17. {
  18. if (args.ApplicationMessage.Topic == message.Topic)
  19. {
  20. tcs.SetResult(true);
  21. }
  22. }
  23. server.ApplicationMessageReceived += Handler;
  24. try
  25. {
  26. await client.PublishAsync(message).ConfigureAwait(false);
  27. await tcs.Task.ConfigureAwait(false);
  28. }
  29. finally
  30. {
  31. server.ApplicationMessageReceived -= Handler;
  32. }
  33. }
  34. }
  35. }