25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

60 satır
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.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. }