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.

WebSocket4NetMqttClientAdapterFactory.cs 1.6 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using MQTTnet.Adapter;
  2. using MQTTnet.Client.Options;
  3. using MQTTnet.Diagnostics;
  4. using MQTTnet.Formatter;
  5. using MQTTnet.Implementations;
  6. using System;
  7. namespace MQTTnet.Extensions.WebSocket4Net
  8. {
  9. public sealed class WebSocket4NetMqttClientAdapterFactory : IMqttClientAdapterFactory
  10. {
  11. readonly IMqttNetLogger _logger;
  12. public WebSocket4NetMqttClientAdapterFactory(IMqttNetLogger logger)
  13. {
  14. _logger = logger ?? throw new ArgumentNullException(nameof(logger));
  15. }
  16. public IMqttChannelAdapter CreateClientAdapter(IMqttClientOptions options)
  17. {
  18. if (options == null) throw new ArgumentNullException(nameof(options));
  19. switch (options.ChannelOptions)
  20. {
  21. case MqttClientTcpOptions _:
  22. {
  23. return new MqttChannelAdapter(
  24. new MqttTcpChannel(options),
  25. new MqttPacketFormatterAdapter(options.ProtocolVersion),
  26. options.PacketInspector,
  27. _logger);
  28. }
  29. case MqttClientWebSocketOptions webSocketOptions:
  30. {
  31. return new MqttChannelAdapter(
  32. new WebSocket4NetMqttChannel(options, webSocketOptions),
  33. new MqttPacketFormatterAdapter(options.ProtocolVersion),
  34. options.PacketInspector,
  35. _logger);
  36. }
  37. default:
  38. {
  39. throw new NotSupportedException();
  40. }
  41. }
  42. }
  43. }
  44. }