Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

TestServerExtensions.cs 1.2 KiB

pirms 6 gadiem
pirms 6 gadiem
1234567891011121314151617181920212223242526272829303132333435363738
  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. EventHandler<MqttApplicationMessageReceivedEventArgs> handler = (sender, 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. }