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.
 
 
 
 

52 lines
1.7 KiB

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