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.
 
 
 
 

51 lines
1.3 KiB

  1. using Microsoft.AspNetCore.Connections;
  2. using MQTTnet.Adapter;
  3. using MQTTnet.Formatter;
  4. using MQTTnet.Server;
  5. using System;
  6. using System.Threading.Tasks;
  7. namespace MQTTnet.AspNetCore.Tests.Mockups
  8. {
  9. public class ConnectionHandlerMockup: IMqttServerAdapter
  10. {
  11. public TaskCompletionSource<MqttConnectionContext> Context { get; } = new TaskCompletionSource<MqttConnectionContext>();
  12. public Func<IMqttChannelAdapter, Task> ClientHandler { get; set; }
  13. public ConnectionHandlerMockup()
  14. {
  15. }
  16. public async Task OnConnectedAsync(ConnectionContext connection)
  17. {
  18. try
  19. {
  20. var writer = new SpanBasedMqttPacketWriter();
  21. var formatter = new MqttPacketFormatterAdapter(writer);
  22. var context = new MqttConnectionContext(formatter, connection);
  23. Context.TrySetResult(context);
  24. await ClientHandler(context);
  25. }
  26. catch (Exception ex)
  27. {
  28. Context.TrySetException(ex);
  29. }
  30. }
  31. public Task StartAsync(IMqttServerOptions options)
  32. {
  33. return Task.CompletedTask;
  34. }
  35. public Task StopAsync()
  36. {
  37. return Task.CompletedTask;
  38. }
  39. public void Dispose()
  40. {
  41. }
  42. }
  43. }