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.
 
 
 
 

61 regels
1.7 KiB

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