Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

TestMqttServerAdapter.cs 1.7 KiB

há 7 anos
há 7 anos
há 6 anos
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using MQTTnet.Adapter;
  5. using MQTTnet.Client;
  6. using MQTTnet.Diagnostics;
  7. using MQTTnet.Server;
  8. namespace MQTTnet.Core.Tests
  9. {
  10. public class TestMqttServerAdapter : IMqttServerAdapter
  11. {
  12. public event EventHandler<MqttServerAdapterClientAcceptedEventArgs> ClientAccepted;
  13. public async Task<IMqttClient> ConnectTestClient(string clientId, MqttApplicationMessage willMessage = null)
  14. {
  15. var adapterA = new TestMqttCommunicationAdapter();
  16. var adapterB = new TestMqttCommunicationAdapter();
  17. adapterA.Partner = adapterB;
  18. adapterB.Partner = adapterA;
  19. var client = new MqttClient(
  20. new TestMqttCommunicationAdapterFactory(adapterA),
  21. new MqttNetLogger());
  22. FireClientAcceptedEvent(adapterB);
  23. var options = new MqttClientOptions
  24. {
  25. ClientId = clientId,
  26. WillMessage = willMessage,
  27. ChannelOptions = new MqttClientTcpOptions()
  28. };
  29. await client.ConnectAsync(options);
  30. SpinWait.SpinUntil(() => client.IsConnected);
  31. return client;
  32. }
  33. private void FireClientAcceptedEvent(IMqttChannelAdapter adapter)
  34. {
  35. ClientAccepted?.Invoke(this, new MqttServerAdapterClientAcceptedEventArgs(adapter));
  36. }
  37. public Task StartAsync(IMqttServerOptions options)
  38. {
  39. return Task.FromResult(0);
  40. }
  41. public Task StopAsync()
  42. {
  43. return Task.FromResult(0);
  44. }
  45. public void Dispose()
  46. {
  47. }
  48. }
  49. }