No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

40 líneas
1.2 KiB

  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. }